Reputation: 742
I have a textbox array in my form. The textbox(s) are dynamically added using javascript function. The text box are named as below:
account[0]_number account[0]_balance
account[1]_number account[1]_balance
How can I get the values of these textboxes using jquery?
Below is how I tried, but it gives error:
if($('#account[' + iteration + ']_balance').val().length==0)
Upvotes: 2
Views: 8891
Reputation: 2432
and if you want to iterate through all the textboxes you could
$("input[type=text][id^=account][value]").each(function(){
//put your code here
})
Upvotes: 2
Reputation: 342635
You could try:
var values = $("input[type=text][id^=account][value]").filter(function() {
return $(this).val();
}).get(); // converts collection to array
I'm not sure if you're referring to name
or id
, if you are referring to name, then modify the selector to input[type=text][name^=account][value]
.
Also, [value]
will only match non-empty elements with a value
attribute.
See http://api.jquery.com/attribute-starts-with-selector/
EDIT this should do it:
$('input[type=text][name=account_' + iteration + '_balance][value]').val()
Upvotes: 6