How to access element id and other attributes using jQuery?

I have a JavaScript method that acts when a specific class (mcb) of forms is submitted:

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

Instead of calling an alert, I need to access the identifier of the form whose request is being called. How can I do it? Even better would be advice for accessing any attribute ...

thanks

+3
source share
2 answers

the identifier of the submitted form will be

this.id      

or in jQuery

$(this).attr('id') //although why type the extra letters?
+7
source
$(".mcb").submit(function(e){
  e.preventDefault();
  $(this).attr("id"); // Returns FORM ID
});

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

+4
source

Source: https://habr.com/ru/post/1715616/


All Articles