Friday, 9 August 2013

jQuery checking relevant checkbox by spliting id values and matching elements accordingly

jQuery checking relevant checkbox by spliting id values and matching
elements accordingly

If I have 4 divs and 4 checkboxes which each have ID's.
<div id="block_1"></div>
<div id="block_2"></div>
<div id="block_3"></div>
<div id="block_4"></div>
<input type="checkbox" id="checkbox_1" />
<input type="checkbox" id="checkbox_2" />
<input type="checkbox" id="checkbox_3" />
<input type="checkbox" id="checkbox_4" />
I want to click the div and check the relevant checkbox. I can get the
number values that I need, but struggling to match the correct checkbox
when I click a div.
$('div').each(function(){
var check = $('input:checkbox');
$(this).click(function(){
var divNum = $this.attr('id').split('block_');
//Need to check the correct checkbox here
});
check.each(function(){
var cid = $(this).attr('id').split('checkbox_');
});
});

No comments:

Post a Comment