I have a checkbox in my page.
<input type="checkbox" id="fruit" value="1" /><label for="fruit">Mango</label>
How to get the associated label value for this CheckBox in JavaScript.
Hio Adnaan,
On CheckBox Click, find the label associated with the CheckBox using id attribute.
Refer below example.
HTML
<html> <head> <meta charset="utf-8" /> <title></title> <style type="text/css"> body { font-family: Arial; font-size: 10pt; } </style> </head> <body> <input type="checkbox" id="fruit" value="1" onclick="ShowMessage(this)" /><label for="fruit">Mango</label> <script type="text/javascript"> function ShowMessage(ele) { if (ele.checked) { var labelElement = document.querySelector("label[for=" + ele.id + "]"); alert(labelElement.innerHTML); } } </script> </body> </html>
Demo
Screenshot
Downloads
Download Sample
© COPYRIGHT 2025 ASPSnippets.com ALL RIGHTS RESERVED.