Hemant
Hemant

Reputation: 19826

How to access element id and other attributes using jQuery?

I have JavaScript method which acts when a particular class (mcb) of forms are submitted:

function BindCloseMessage() {
    $(".mcb").submit(function(event) {
        alert("closing..."); //Here I want to get the id of form
        event.preventDefault();
    });
}

In place of alert call, I need to access the id of form whose submit is called. How can I do it? Even better will be the tip for accessing any attribute...

Thanks

Upvotes: 3

Views: 5762

Answers (2)

redsquare
redsquare

Reputation: 78667

the id of the form being submitted will be

this.id      

or in jQuery

$(this).attr('id') //although why type the extra letters?

Upvotes: 7

Sampson
Sampson

Reputation: 268344

$(".mcb").submit(function(e){
  e.preventDefault();
  $(this).attr("id"); // Returns FORM ID
});

You can learn more about jQuery's attribute-methods at http://docs.jquery.com/Attributes

Upvotes: 4

Related Questions