Click here to Skip to main content
15,914,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have multiple

<input id="ChildidIntl1" class="onlyforage" type="text" name="ageperwise[1]"></input>
<input id="ChildidIntl1" class="onlyforage" type="text" name="ageperwise[1]"></input>
<input id="ChildidIntl1" class="onlyforage" type="text" name="ageperwise[1]"></input>
<input id="ChildidIntl1" class="onlyforage" type="text" name="ageperwise[1]"></input>



how to get value from each on button click
Posted
Comments
Anurag Sinha V 13-Jan-14 0:06am    
How come all the id's are same?

1 solution

Quote:

HTML
<input id="ChildidIntl1" class="onlyforage" type="text" name="ageperwise[1]"></input>

<input id="ChildidIntl1" class="onlyforage" type="text" name="ageperwise[1]"></input>

<input id="ChildidIntl1" class="onlyforage" type="text" name="ageperwise[1]"></input>

<input id="ChildidIntl1" class="onlyforage" type="text" name="ageperwise[1]"></input>
First of all, all the TextBox Id are same, which can't be possible. Id is a attribute which should be different from all other elements on the page.

So, if you change the code to...
HTML
<input id="ChildidIntl1" class="onlyforage" type="text" name="ageperwise[1]"></input>

<input id="ChildidIntl2" class="onlyforage" type="text" name="ageperwise[1]"></input>

<input id="ChildidIntl3" class="onlyforage" type="text" name="ageperwise[1]"></input>

<input id="ChildidIntl4" class="onlyforage" type="text" name="ageperwise[1]"></input>

Then getting the value is easy in jQuery.

Value by Selecting IDs


It is like $('#ChildidIntl1').val();.

Demo - [Demo] Value from TextBoxes on Button Click[^]

Value by Selecting name


If you want to use "name" attribute to get the value, then you have to select each of the TextBox with the help of Attribute Equals Selector [name="value"][^].

So, code would look like...
JavaScript
var allValues = "";

$('input[name="ageperwise[1]"]').each(function(){
    allValues += $(this).val() + "\n";
});


Demo - [Demo] Value from TextBoxes (having same name) on Button Click[^]

Value by Selecting class


As per Karthik's comment, I am also giving the Demo of selecting the value by class name.

Demo - [Demo] Value from TextBoxes (having same class) on Button Click[^]
 
Share this answer
 
v6
Comments
Karthik_Mahalingam 13-Jan-14 0:32am    
5!
Add this also,
might be useful to the OP
$(.onlyforage').val()
Thanks a lot KARTHIK... :)

I have updated my answer to select the TextBoxes by name attribute.

$(.onlyforage').val() will select only one value that is by default the first TextBox.
If you want to get all TextBox values, then you have to loop using .each method as I did it for name attribute.
I have also given the Demo for getting the value by class name of TextBoxes.
Take a look.

Thanks,
Tadit
Karthik_Mahalingam 13-Jan-14 1:33am    
Good,
Thanks for considering my input..
and responded as well.
:)
Most welcome and thanks for suggesting one alternative. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900