James
James

Reputation: 14111

How to Get id's of all inputs inside the form?

How to get all id's of input elements inside a form in an array?

Upvotes: 19

Views: 26411

Answers (3)

Amber
Amber

Reputation: 526763

$ids = $('#myform input[id]').map(function() {
  return this.id;
}).get();

Upvotes: 18

Leniel Maccaferri
Leniel Maccaferri

Reputation: 102428

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>

Upvotes: 17

FelipeAls
FelipeAls

Reputation: 22171

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'));
});
});

Upvotes: 5

Related Questions