css - Jquery to change div background color when radio button selected? -


i have 2 separate sets of radio buttons , want its' divs' color change when selected. behavior want quite straightforward, if radio button selected, corresponding div should change color. (and change original color if deselected).

however, issue if radio button in set selected, first set's div changes original color though 1 of radio buttons still selected. how fix this?

see demo here

jquery used:

$(document).ready(function(){     $('input:radio').change(function(){         $('div.highlight').removeclass('highlight');         $(this).closest('div').addclass('highlight');     }); }); 

for 2nd issue need wrap your's inputs block separate wrapper , change bit of code..

js

$(document).ready(function(){     $('input:radio').change(function(){         var $this = $(this);         // remove class in specific `box` contains radio         $this.closest('.box').find('div.highlight').removeclass('highlight');         $this.closest('.q').addclass('highlight');     }); }); 

html

<div class="box"> 1st set     <div class="q">         <input type="radio" id="1" name="11" />         <label 1>a</label>     </div>     <div class="q">         <input type="radio" id="2" name="11" />         <label 2>b</label>     </div> </div> <div class="box">     <hr> 2nd set     <div class="q">         <input type="radio" id="3" name="22" />         <label 3>a</label>     </div>     <div class="q">         <input type="radio" id="4" name="22" />         <label 4>b</label>     </div> </div> 

check fiddle


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -