By event.target, how I know that it is checkbox or it is radio?

78,447

Solution 1

.nodeName gives the html tag used so you have to use .type to get the type of the node there.

Try this one:

console.log(event.target.type);

Demo

Solution 2

For those of you looking for the pure JavaScript way, it's event.target.tagName.

That property is an uppercase string of the element type, like "A", "UL", "LI", etc.

Solution 3

console.log($(event.target).is(':radio'));
console.log($(event.target).is('[type="radio"]'));

Solution 4

you can use event.target.getAttribute('type') instead

Share:
78,447
Rituraj ratan
Author by

Rituraj ratan

I am a self-motivated person who loves challenges. I am a team member with good communication skills and analytical skills. I don't let my work pile up. I am a good time manger with strong attitude toward work. I have cooperative nature, and have ability to work with team as well as individual. Specialties ● Object Oriented PHP 5 / MySQL web developer ● Windows, MVC, JQuery, HTML,ajax and YII ● Project management experience ● FaceBook App profile for Rituraj ratan on Stack Exchange, a network of free, community-driven Q&A sites http://stackexchange.com/users/flair/968224.png http://stackoverflow.com/questions/18996228/finding-elements-with-one-class-and-without-another-class/18996242#18996242 http://stackoverflow.com/questions/20257963/how-to-check-div-has-script/20257968#20257968 http://stackoverflow.com/questions/18357870/using-this-on-objects/18357894#18357894 http://maddyzone.com http://tech-blog.maddyzone.com https://stackoverflow.com/story/riturajratan

Updated on December 24, 2020

Comments

  • Rituraj ratan
    Rituraj ratan over 3 years

    In my html:

    <input type="checkbox" name="select">
    
    <input type="radio" name="select">
    

    name property same due to some logical reason

    in my JS

    I write below code:

    $('input[type="checkbox"],input[type="radio"]').on("change",function(event){
        console.log(event.target.nodename);
        // on both cases it show "INPUT"
        }
    
        }); 
    

    How I will know that I click on checkbox or radio button?