jquery - Using delegate to swap classes -


i using jquery delegate function toggle classes no success , want toggle class of

<li class="unselected-values"> 

to

<li class="<li class="unselected-values"> 

when client clicks on label, ideas m getting wrong , suggestion or assistance appreciated. thnx

    <ul class="option-list swatch pockets">                             @foreach (var pvavalue in attribute.values)                             {                                                                     <li class="unselected-values">                                     <input id="@(controlid)_@(pvavalue.id)" type="checkbox" value="@pvavalue.id" checked="@pvavalue.ispreselected" />                                     <label for="@(controlid)_@(pvavalue.id)" style="background-image:url(@(pvavalue.menuicon))">@pvavalue.name</label>                                 </li>                             }                         </ul>  <script type="text/javascript">                       $(document).ready(function() {                           $('.option-list swatch pockets').delegate('label', 'click', function(event) {                               $(this).parent().toggleclass('selected-value');                               console.log($(this).parent);                               alert($(this).parent);                               return false;                            });                         });                                 </script> 

your selector $('.option-list swatch pockets') wrong, means element pockets inside swatch element inside element class option-list.

try

$('.option-list.swatch.pockets') 

it means element classes option-list, swatch , pockets.

also in order toggle between 2 classes, need pass both classes toggleclass method.

$(document).ready(function() {     $('.option-list.swatch.pockets').delegate('label', 'click', function(event) {         $(this).parent().toggleclass('unselected-values selected-value');         return false;     }); }); 

demo: fiddle


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -