Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery click label+checkbox detect which?

Tags:

jquery

I am trying to check the event.target.nodeName as follows:

$("input").click(function(e){
    if(e.target.nodeName == "LABEL") {
       alert('label click');
        e.preventDefault();
    } else {
       alert($(this).attr('checked') ? 'checked': 'unchecked');
    }
});

But the name never equals label? What am I doing wrong?

Quick jsfiddle

like image 395
John Magnolia Avatar asked Feb 20 '23 01:02

John Magnolia


2 Answers

You should select the label(parent) element. Currently the only target of your click handler is the input element:

$("label").click(function(e){
  // ...
})

http://jsfiddle.net/j7nSq/

like image 143
undefined Avatar answered Apr 10 '23 16:04

undefined


I think the reason this doesn't work is because this will only run if you click on input:

  if(e.target.nodeName == "LABEL") {
like image 23
user1695032 Avatar answered Apr 10 '23 16:04

user1695032