1

I have some input fields in the following format:

<input name='date_of_birth[month]' type='text' />
<input name='date_of_birth[day]' type='text' />
<input name='date_of_birth[year]' type='text' />

Is there a way to select all the values of these fields in jQuery?

0

2 Answers 2

5

The $.map method may be better in this case:

var dobArray = $.map($('input[type=text][name^=date_of_birth]'),function(){
  return this.value;
});

and to make it a date string,

var dobString = dobArray.join("/");
Sign up to request clarification or add additional context in comments.

1 Comment

Keep in mind that the order of the array will be based on the DOM order of the input elements.
2
$(":text[name^='date_of_birth']").each(function (){alert(this.value)});

http://jsbin.com/emumef/edit#javascript,html

according to @gordon about speed - this will be faster: ( reduce extension overhead)

 $("input[type='text'][name^='date_of_birth']")

5 Comments

+1, note that :text isn't efficient selector. you should replace it with input[type='text']
@gdoron input type text is better ?
"Because :text is a jQuery extension and not part of the CSS specification, queries using :text cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. For better performance in modern browsers, use [type="text"] instead."
One more thing if I may, Though $() with DOM element isn't expensive, you should use this.value instead of $(this).val().
@gordon, yeah i know . its an instinct ...:)

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.