Reputation: 529
I have two array checkboxes namely chk1 & chk2 this checkboxes data are generated dynamically. When i check chk1 UK means chk2 90 has to be checked. if US means 60 has to be checked and so on. any suggestion to do this task?
<input type="checkbox" name="chk1" id="chk1" value="UK"> UK
<input type="checkbox" name="chk1" id="chk1" value="US"> US
<input type="checkbox" name="chk1" id="chk1" value="IN"> IN
<input type="checkbox" name="chk2" id="chk2" value="90"> 90
<input type="checkbox" name="chk2" id="chk2" value="60"> 60
<input type="checkbox" name="chk2" id="chk2" value="10"> 10
Upvotes: 4
Views: 1773
Reputation: 20740
You can do it like following. Hope this will help you.
$("input[type=checkbox][name=chk1]").change(function () {
var status = this.checked;
$("input[type=checkbox][name=chk2]").eq($(this).index()).prop('checked', status);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="chk1" id="chk1" value="UK"> UK
<input type="checkbox" name="chk1" id="chk1" value="US"> US
<input type="checkbox" name="chk1" id="chk1" value="IN"> IN
<input type="checkbox" name="chk2" id="chk2" value="90"> 90
<input type="checkbox" name="chk2" id="chk2" value="60"> 60
<input type="checkbox" name="chk2" id="chk2" value="10"> 10
Upvotes: 0
Reputation: 529
My own solution for this task
$(document).on('click' ,'input[name=chk1]', function(){
$("input[name=chk1]").each(function(i) {
if($(this).is(":checked"))
$('input[name=chk2]:eq('+i+')').prop('checked' , true);
else
$('input[name=chk2]:eq('+i+')').prop('checked' , false);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<input type="checkbox" name="chk1" id="chk1" value="UK"> UK
<input type="checkbox" name="chk1" id="chk1" value="US"> US
<input type="checkbox" name="chk1" id="chk1" value="IN"> IN
<br>
<input type="checkbox" name="chk2" id="chk2" value="90"> 90
<input type="checkbox" name="chk2" id="chk2" value="60"> 60
<input type="checkbox" name="chk2" id="chk2" value="10"> 10
Upvotes: 0
Reputation: 24001
while you tagged Jquery you can use .eq()
and .index()
$('input[name=chk1]').on('click' , function(){
if($(this).is(":checked")){
$('input[name=chk2]').eq($(this).index()).prop('checked' , true);
}else{
$('input[name=chk2]').eq($(this).index()).prop('checked' , false);
}
});
Note: be sure to include jquery
Another way using :eq()
selector with .index()
$('input[name=chk2]:eq('+$(this).index()+')')
Upvotes: 3
Reputation: 4225
Pure JavaScript
<input onclick='handleClick(this);' type="checkbox" name="chk1" id="chk1" value="UK"> UK
<input onclick='handleClick(this);' type="checkbox" name="chk2" id="chk2" value="US"> US
<input onclick='handleClick(this);' type="checkbox" name="chk3" id="chk3" value="IN"> IN
<input type="checkbox" name="chk4" id="chk4" value="90"> 90
<input type="checkbox" name="chk5" id="chk5" value="60"> 60
<input type="checkbox" name="chk6" id="chk6" value="10"> 10
<script>
function handleClick(cb) {
switch (cb.name) {
case "chk1":
document.getElementById("chk4").checked = cb.checked;
break;
case "chk2":
document.getElementById("chk5").checked = cb.checked;
break;
case "chk3":
document.getElementById("chk6").checked = cb.checked;
break;
}
}
</script>
Upvotes: 0