最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - event.target.nodeName != "CHECKBOX" - Stack Overflow

programmeradmin2浏览0评论

I'm trying to get a function work properly. When clicking on a row in a table, a pop up appears with the info(from the clicked row) auto filled in into textfields. This part works perfectly.

What i want to do now is that the function gets called ONLY if in the row it's not clicked on a button or a checkbox. The button part works, but not the checkbox.

It seems that event.target.nodeName != "CHECKBOX" isn't working.

Why doesn't he recognize the nodeName "checkbox"? Or am i doing it wrong ? I already searched the internet to find the nodeName for a checkbox. But no results :/

Sorry for my bad English!

    $('.management table.table-panies tbody tr').on('click touchstart',function(event) {
        if(
            event.target.nodeName != "BUTTON" 
            && event.target.nodeName != "I"
            && event.target.nodeName != "CHECKBOX"
            )
        {

            jmOpenRelatedContact($(this));

        }
    });

I'm trying to get a function work properly. When clicking on a row in a table, a pop up appears with the info(from the clicked row) auto filled in into textfields. This part works perfectly.

What i want to do now is that the function gets called ONLY if in the row it's not clicked on a button or a checkbox. The button part works, but not the checkbox.

It seems that event.target.nodeName != "CHECKBOX" isn't working.

Why doesn't he recognize the nodeName "checkbox"? Or am i doing it wrong ? I already searched the internet to find the nodeName for a checkbox. But no results :/

Sorry for my bad English!

    $('.management table.table-panies tbody tr').on('click touchstart',function(event) {
        if(
            event.target.nodeName != "BUTTON" 
            && event.target.nodeName != "I"
            && event.target.nodeName != "CHECKBOX"
            )
        {

            jmOpenRelatedContact($(this));

        }
    });
Share Improve this question asked Mar 13, 2015 at 21:43 user3788334user3788334 312 silver badges8 bronze badges 1
  • 3 Because the nodeName of <input type="checkbox"/> is "INPUT", not "CHECKBOX". – blex Commented Mar 13, 2015 at 21:45
Add a ment  | 

3 Answers 3

Reset to default 3

You should use event.target.type instead of event.target.nodeName for input.

See the code:

var ctype = document.getElementById("c").type;
document.write("type="+ctype)
<input id="c" type="checkbox">

If you allow clicks on inputs that are not checkboxes, using nodeName != "INPUT" won't be right. In this case, you can use getAttribute() to find out if the type is "checkbox":

$('.management table.table-panies tbody tr').on('click touchstart',function(event) {
    if(
        event.target.nodeName != "BUTTON" 
        && event.target.nodeName != "I"
        && !(event.target.nodeName == "INPUT" && event.target.getAttribute('type') == "checkbox")
        )
    {

        jmOpenRelatedContact($(this));

    }
});

Edit: Check out RoliCo's answer, it's a good one.

The node name is 'INPUT', because a checkbox is defined like the following :

<input type="checkbox" name="animal" value="Cat" />
^^^^^
We are using an input element.

You could have figured that out by alerting the value of

event.target.nodeName

See by yourself : fiddle

发布评论

评论列表(0)

  1. 暂无评论