ctrlShiftBryan
ctrlShiftBryan

Reputation: 27740

jQuery: .each how to reference matched object

how do i get a reference to the element that matched? 'this' is not correct.

For example here I am trying to alert with the elements value.

//how do i get this to reference the object it matches.
$("#txtFirstName,#txtLastName,#txtDOB").each(
    function() { alert(this.val()); }
);

Upvotes: 1

Views: 2550

Answers (3)

Naeem Sarfraz
Naeem Sarfraz

Reputation: 7430

In that context this is a reference to the actual DOM element. You will need to wrap that into a jQuery object in order to use the val() function.

$(this).val()

Upvotes: 1

user113716
user113716

Reputation: 322492

It is this, but to use val() you need a jQuery object.

$(this).val();

Upvotes: 2

brettkelly
brettkelly

Reputation: 28205

Try:

$("#txtFirstName,#txtLastName,#txtDOB").each(
    function() { alert( $(this).val() ); }
);

Upvotes: 7

Related Questions