swg1cor14
swg1cor14

Reputation: 1722

Iterate Through Jquery Form Object to get inputs

I'm passing a jQuery form object to a function. This function should look at all the checkboxes and find the ID attribute.

I tried the filter function. But its not seeing any input fields.

function templateRemove(form){

    selectedTemplateSizes = [];

    var selectedSizesCount = 0;

    form.filter(':input').each(function(i, e){
        console.log($(e));
        selectedTemplateSizes.push($(e).attr('id'));
    });

}

But nothing is getting added to the array and nothing gets logged in console. The call to this function is as follows:

 templateRemove($( "#delete-selected-form" ));

Am I not doing this right?

Upvotes: 1

Views: 75

Answers (2)

bpositive
bpositive

Reputation: 70

This might work for you.

Modify the function as:

function templateRemove(form){
    selectedTemplateSizes = [];
    var selectedSizesCount = 0;
    $(form + ' :input').each(function(i, e){
        var inputField = $(this);
        //now you can do whatever you want to do with this input field
    });
}

And call it as:

templateRemove( "#delete-selected-form" );

i.e, just pass the id of that element as a string and not the object itself, for easy understanding.

Upvotes: 3

JF it
JF it

Reputation: 2403

Use Find instead of Filter.. , it works fine..

You can think of it this way : Find - Searches selected items for matched elements, whereas Filter removes unmatched elements from selected..

templateRemove($( "#delete-selected-form" ));

function templateRemove(form){

selectedTemplateSizes = [];

var selectedSizesCount = 0;

  form.find(':input').each(function(i, e){
    console.log($(e));
    selectedTemplateSizes.push($(e).attr('id'));
  });

}

See this fiddle: http://jsfiddle.net/jFIT/m9QqX/

Upvotes: 1

Related Questions