views:

454

answers:

2

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

+3  A: 

the id of the form being submitted will be

this.id

or in jQuery

$(this).attr('id') //although why type the extra letters?
redsquare
Additionally `$` would be called, that would create a new jQuery object of `this` and then its `attr` method would be called.
Gumbo
indeed, but i thought that was obvious;)
redsquare
+2  A: 
$(".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

Jonathan Sampson