Samantha J T Star
Samantha J T Star

Reputation: 32768

How can I tie together a jQuery object and a selector?

I have the following where $form is a jQuery object.

var $form  = oSubmit.$form;

On this form I have input elements with a class of update-grid.

        $('input.update-grid')
            .each(function () {
                var id = this.id.replace('modal', '');
                $('#input' + id).val(this.value)
            })

How can I tie these two together so that the function works on each input.update-grid on the $form object?

Upvotes: 0

Views: 75

Answers (3)

Xmindz
Xmindz

Reputation: 1282

Try this:

$form.find('input.update-grid').each(function () {

var id = this.id.replace('modal', '');

$('#input' + id).val(this.value)

})

Upvotes: 0

Alnitak
Alnitak

Reputation: 339786

You can use

$('input.update-grid', $form).each(...)

which is actually just a wrapper for:

$form.find('input.update-grid').each(...)

Upvotes: 0

adeneo
adeneo

Reputation: 318182

Use the object in $form as context :

$('input.update-grid', $form).each(function(index, elem) {
    var id = elem.id.replace('modal', '');
    $('#input' + id).val(elem.value)
});

Upvotes: 3

Related Questions