html - Get the checkbox in which div was clicked using javascript? -


if have 2 divs checkboxes in each of them, how can find out checkbox in div clicked?

<div id="num-1">    <input type="checkbox" name="check-1" value="check-1">checkbox "check-1" of div "num-1"    <br>  </div>    <div id="num-2">    <input type="checkbox" name="check-1" value="check-1">checkbox "check-2" of div "num-2"    <br>  </div>

i grateful if advise me on method uses js (not jquery).

one way use event delegation , name attribute. once target checkbox can access object including it's value, name or if it's selected or not

document.queryselector('.someform').addeventlistener('click', clickhandler);    function clickhandler(e) {              var input = e.path.filter(function (x) {return x.type === 'checkbox'});        if(input.length) {          console.log(input.pop().parentnode);      }    }
<form class="someform">  <div id="num-1">  <input type="checkbox" name="check-1" value="check-1"> checkbox "check-1" of div "num-1" <br>  </div>    <div id="num-2">  <input type="checkbox" name="check-2" value="check-1"> checkbox "check-2" of div "num-2" <br>  </div>  </form>


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -