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?

GSto
  • 40,278
  • 37
  • 127
  • 179

2 Answers2

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("/");
Kevin B
  • 94,164
  • 15
  • 164
  • 175
  • 1
    Keep in mind that the order of the array will be based on the DOM order of the input elements. – Kevin B May 10 '12 at 20:22
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']")
Royi Namir
  • 138,711
  • 129
  • 435
  • 755