I have a group of checkboxes as -
<input id="check_1" name="check[1]" type="checkbox" value="1"/>
<label for="check_1">One</label>
<input id="check_2" name="check[2]" type="checkbox" value="1"/>
<label for="check_2">Two</label>
<input id="check_3" name="check[3]" type="checkbox" value="1"/>
<label for="check_3">Three</label>
Due to variable values of id
and name
, I'm unable to handle onclick event on checkbox with label One
.
I've tried this which works fine, but I don't want to use check_1
since the number 1 is variable and could be changed.
$("#check_1").change(function() {
alert('Checkbox One is clicked');
});
How do I do this as I have no access to modify the html ?
I have a group of checkboxes as -
<input id="check_1" name="check[1]" type="checkbox" value="1"/>
<label for="check_1">One</label>
<input id="check_2" name="check[2]" type="checkbox" value="1"/>
<label for="check_2">Two</label>
<input id="check_3" name="check[3]" type="checkbox" value="1"/>
<label for="check_3">Three</label>
Due to variable values of id
and name
, I'm unable to handle onclick event on checkbox with label One
.
I've tried this which works fine, but I don't want to use check_1
since the number 1 is variable and could be changed.
$("#check_1").change(function() {
alert('Checkbox One is clicked');
});
How do I do this as I have no access to modify the html ?
Share Improve this question edited Dec 6, 2016 at 12:03 Aruna 12k3 gold badges30 silver badges42 bronze badges asked Dec 6, 2016 at 11:28 jitendrapurohitjitendrapurohit 9,6852 gold badges30 silver badges41 bronze badges 7-
use
this
keyword – Mahi Commented Dec 6, 2016 at 11:30 -
try
$('input[type="checkbox"]')
– Dev Man Commented Dec 6, 2016 at 11:30 -
$("input[id^='check_']").change()
– philantrovert Commented Dec 6, 2016 at 11:31 - @philantrovert In that case how will I check the next label is one which is being checked ? – jitendrapurohit Commented Dec 6, 2016 at 11:31
-
@jitendrapurohit us
this
inside the function. – philantrovert Commented Dec 6, 2016 at 11:32
2 Answers
Reset to default 5You can use a selector like this $('input[type="checkbox"]:has(+ label:contains("One"))')
with the label text as below,
$('input[type="checkbox"]:has(+ label:contains("One"))').on('click', function(){
alert('Checkbox One is clicked');
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="check_1" name="check[1]" type="checkbox" value="1"/>
<label for="check_1">One</label>
<input id="check_2" name="check[2]" type="checkbox" value="1"/>
<label for="check_2">Two</label>
<input id="check_3" name="check[3]" type="checkbox" value="1"/>
<label for="check_3">Three</label>
Looks like your only criteria is the text of the label
so you could target the input based on that label like :
$('label:contains("One")').prev('input:checkbox').change(function(){
console.log('One changed');
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="check_1" name="check[1]" type="checkbox" value="1"/>
<label for="check_1">One</label>
<input id="check_2" name="check[2]" type="checkbox" value="1"/>
<label for="check_2">Two</label>
<input id="check_3" name="check[3]" type="checkbox" value="1"/>
<label for="check_3">Three</label>
Hope this helps.