Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find target is button in Jquery?

Tags:

html

jquery

In document click event how to return if target is button element

$(document).click(function(e){
    if(e.target.nodeName.toLowerCase() != 'button')

Whether above code was correct ?

like image 884
Ragavan Avatar asked Jul 01 '13 10:07

Ragavan


2 Answers

You may do only

$(document).click(function(e) {
  if ( $( e.currentTarget ).is( ":button" ) ) {
    // Do things
  }
});

Why would you use :button instead of button?

Because that way you can detect if it's a <input type="button"> OR a <button> tag, aswell the other input types which render as buttons.
If you're unsure about using this selector, check the :button selector jQuery docs.

like image 120
gustavohenke Avatar answered Oct 02 '22 12:10

gustavohenke


You can use .is() to test the given element against a selecor.

Also you can use :button selector to test, if you want only element button then you can use is('button') element selector

$(document).click(function(e) {
  if ($( e.target ).is(":button")) {
    //check
  }
});
like image 27
Arun P Johny Avatar answered Oct 02 '22 14:10

Arun P Johny