How to get all id's of input elements inside a form in an array?
+4
A:
$ids = $('#myform input[id]').map(function() {
return this.id;
}).get();
Amber
2010-06-05 17:56:53
+1 - `map()` is the way to go, although if there are `input` elements that don't have an ID (perhaps a Submit), you'll end up with an empty entry in the array. You may want to change the selector to: `$('#test input[id]')`, or at least provide a test like: `if(this.id) return this.id;`
patrick dw
2010-06-05 18:41:07
Good suggestion, patrick - added to the answer.
Amber
2010-06-05 20:28:28
Thank you.......very much
Vipin
2010-06-06 06:14:22
A:
Something along the lines...
<script src="../../Scripts/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function ()
{
// Get all the inputs into an array...
var $inputs = $('#myForm :input');
// An array of just the ids...
var ids = {};
$inputs.each(function (index)
{
// For debugging purposes...
alert(index + ': ' + $(this).attr('id'));
ids[$(this).attr('name')] = $(this).attr('id');
});
});
</script>
Leniel Macaferi
2010-06-05 17:58:31
A:
You can narrow your search with a more precise selector : form input and an attribute selector for the ones having an id
$(document).ready(function() {
$('form input[id]').each(function() {
formId.push(J(this).attr('id'));
});
});
Felipe Alsacreations
2010-06-05 18:10:13