简体   繁体   中英

How can I pass checkbox onclick event to parent onclick?

I a with an onclick event and , and inside it.
When I click on the checkbox then the div-onclick event doesnt fire - I would like it to fire without having to dublicate the onclick onto the checkbox.

<div id="id-tag" onclick="do()">
  <span>text</span>
  <img src="pic.jpg" />
  <input type="checkbox" name="mycheck" />
</div>

I know the div-idtag but ideally would like to avoid specifying it inside the checkbox.
Using another function call on the checkbox onclick to call the parent is NOT what I am looking for, like

onclick="run_parent_onclick()"

but this is okay (untested)

onclick="this.parent.click()"

Use addEventListener:

function do(e) { ... }
document.getElementById('id-tag').addEventListener('click', do, true);

The "true" makes the event capture.

You could fire the div's click event manually in the input's onclick:

<input type="checkbox" name="mycheck" onclick="this.parentNode.click()" />

or

<input type="checkbox" name="mycheck" onclick="document.getElementById('id-tag').click()" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM