jquery - Change all radio buttons, my code only changes the first one? -


using jquery changing radio input's (that toggle switch) color, when on green, when off black..

fiddle: http://jsfiddle.net/sdhfp/1/ (only first, color changes "responsive" other 1 changes when first 1 does.. not own change function)

here html:

<fieldset class="buttonset">   <span class="toggle-bg" id="responsive" style="background-color: rgb(70, 182, 146);">    <input type="radio" checked="checked" value="1" class="switch-input" name="responsive" id="responsive_0">   <input type="hidden" value="responsive" class="switch-id-value">    <input type="radio" value="0" class="switch-input" name="responsive" id="responsive_1">   <input type="hidden" value="responsive" class="switch-id-value">    <span class="switch ui-buttonset"></span></span> </fieldset> 

the name value , input id dynamic toggle radio button's name value , input id unique...

here jquery: targeting first radio button happens markup above responsive first radio button in many buttons have...

jquery('.toggle-bg').on('change', function () { var value  = jquery('.toggle-bg input.switch-id-value').val(),     moon1  = jquery('#' + value + '_0').is(':checked'),     slider = jquery('._moon_staticarea_height'),     toggle = jquery('.toggle-bg ');  toggle.css('background-color', (moon1 ? '#46b692' : '#333')); slider[moon1?'slideup':'slidedown'](); }).trigger('change'); 

i cant figure out how change id of dynamically created button? tried making element selector output...

fiddle: http://jsfiddle.net/sdhfp/1/

you need use this instead of referring class within event.

jquery('.toggle-bg').on('change', function () {     var value = jquery('input.switch-id-value',this).val(),         moon1 = jquery('#' + value + '_0').is(':checked'),         slider = jquery('._moon_staticarea_height'),         toggle = jquery(this);      toggle.css('background-color', (moon1 ? '#46b692' : '#333'));     slider[moon1 ? 'slideup' : 'slidedown'](); }).trigger('change'); 

jsfiddle example


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -