tags:

views:

237

answers:

2

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)
+3  A: 

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()
karim79
actually I want to access a specific account number or balance at a time. So, converting to array every time i need to access only one account, seems a bit inefficient?I there a way I can just access a single textbox, if the index number is known?
@maxxxee - Wow, I had understood something *completely* different. I've updated the answer.
karim79
that worked, can you tell me if it is a dropdown in place of textbox in same scenario, how can i read it?
@maxxxee - You can use `$('select[name=account_' + iteration + '_balance][value]').val()`
karim79
A: 

and if you want to iterate through all the textboxes you could

$("input[type=text][id^=account][value]").each(function(){
    //put your code here
})
kaklon