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)
1
  • using # is for id references, not names. Commented Feb 15, 2010 at 11:51

2 Answers 2

6

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()
Sign up to request clarification or add additional context in comments.

4 Comments

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.
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()
2

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.