Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to iterate all elements based on name attribute and modify the value in Jquery?

Below is my HTML snippet,

<div class="direct-service">
    ..   
    <input handle="input" type="hidden" name="./season" value="spring">
    <input handle="input" type="hidden" name="./season" value="">
    ...
</div>

I want to iterate all element inside direct-service class and look for ./season name attribute and check if the value is empty. If it is empty I need to modify the element as

<input handle="input" type="hidden" name="./season@Empty" value="">

I tried to get the value based name property with below code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$("input[name='./season']").val();

But it is giving me the value for first element and not giving me the second element. Seemed like I need to scan all element inside the div class. Could you anyone tell me the best way to manage this case?

>Solution :

You can use .eq() function, example:

$("input[name='./season']").eq(0).val(); // Get first element
$("input[name='./season']").eq(1).val(); // Get second element
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading