EN
JavaScript - select checkbox on click on related text
0
points
In this article, we would like to show you how to select and unselect checkbox on click on related text using JavaScript.
Quick solution:
// ONLINE-RUNNER:browser;
<div>
<input type="checkbox"/>
<span onclick="this.previousElementSibling.click();">Click here to check input</span>
</div>
Practical example
In this example, we simulate a click event on the previousElementSibling
property of a label (span element) that is right after the input
that we want to check/uncheck.
// ONLINE-RUNNER:browser;
<!doctype html>
<html>
<body>
<div>
<input type="checkbox"/>
<span onclick="this.previousElementSibling.click();">Click here to check input</span>
</div>
</body>
</html>