Search code examples
javascripthtmljqueryformsradio-button

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


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?


Solution

  • .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