javascript - JQuery "onchange" event on just one checkbox among multiple in a group based on label text - Stac

I have a group of checkboxes as -<input id="check_1" name="check[1]" type="

I have a group of checkboxes as -

<input id="check_1" name="check[1]" type="checkbox" value="1"/>
<label for="check_1">One</label>&nbsp;

<input id="check_2" name="check[2]" type="checkbox" value="1"/>
<label for="check_2">Two</label>&nbsp;

<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>&nbsp;

<input id="check_2" name="check[2]" type="checkbox" value="1"/>
<label for="check_2">Two</label>&nbsp;

<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
 |  Show 2 more ments

2 Answers 2

Reset to default 5

You 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>&nbsp;

<input id="check_2" name="check[2]" type="checkbox" value="1"/>
<label for="check_2">Two</label>&nbsp;

<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>&nbsp;

<input id="check_2" name="check[2]" type="checkbox" value="1"/>
<label for="check_2">Two</label>&nbsp;

<input id="check_3" name="check[3]" type="checkbox" value="1"/>
<label for="check_3">Three</label>

Hope this helps.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745168154a4614759.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信