jQuery add/remove/toggle classes to non-parent div from radio selection -
having trouble trying dynamically add , remove classes div based on radio selection. essentially, i'm attempting have second header area can dynamically changed based on radio selections.
the html:
<!-- headers --> <div class="header"> <p>i'm header</p> </div> <div class="extra-header header-hidden"> <p>i'm header</p> </div> <!-- radio buttons --> <div class="radio-inline"> <label> <input type="radio" name="headerchoice" class="trigger" id="headeroptionactivate" value="activate" checked /> off </label> </div> <div class="radio-inline"> <label> <input type="radio" name="headerchoice" class="trigger" id="headeroptionsmall" value="small" /> s </label> </div> <div class="radio-inline"> <label> <input type="radio" name="headerchoice" class="trigger" id="headeroptionmedium" value="medium" /> m </label> </div> <div class="selection-wrap"> <div class="radio-inline"> <label> <input type="radio" name="headerchoice" class="trigger" id="headeroptionlarge" value="large" /> l </label> </div> </div>
the javascript (using jquery)
$('.trigger').change(function(){ $('.extra-header').removeclass('header-hidden'); $('.extra-header').toggleclass('header-' + $(this).val()); });
here's fiddle of problem: http://jsfiddle.net/a3zes/1/
as can see, classes being added, they're not being removed. i've tried numerous variations of jquery haven't been able figure out. obliged insight.
you're looking this
$('.trigger').on('change', function () { $('.extra-header').toggleclass('header-hidden', $('#headeroptionactivate').is(':checked')); $('.trigger').each(function() { $('.extra-header').toggleclass('header-' + this.value, this.checked); }); });
Comments
Post a Comment