How to get field values dynamically through ID using jQuery?

Let us consider a scenario, There are n number of fields with different ids.We need to get the value of all the fields dynamically without specifying id explicitly.

For Example, lets have following html tags in our html form:

<input type="text" id="id_1" value="rosary"/>
<input type="text" id="id_2" value="prabhu"/>
<input type="text" id="id_3" value="stephen"/>
<input type="text" id="id_4" value="ilango"/>
<input type="text" id="id_5" value="sathya"/>

We need to get the values of each text box fields dynamically without specifying each ids explicitly.

Following is the code snippet to achieve the above task:

for (var i=1; i<=5; i++) {
    var test = $("input[id='id_" + i + "']").val();
    alert (test);
}

You can also achieve the same using following code snippet

$("input[type='text']").each(function(){
    var test = $("input[id='"+this.id+"']").val();
});

Share This Post

Related Articles

Leave a Reply

© 2017 Techy Diary. All rights reserved.
Powered by Charvi Groups