javascript - How to only change toggle image of child element? -
i have plus/minus image toggle using jquery, when click on ul
, every plus/minus image changes @ same time. need change image ul clicked on.
jquery:
$("#xmldiv").on("click", "ul", function(e) { $(this).find(".mainlist").slidetoggle('slow', function () { if($('span.minus img').attr('src') == 'images/plus.png') $('span.minus img').attr('src', "images/minus.png"); else $('span.minus img').attr('src', "images/plus.png"); }); });
html:
<div id="xmldiv"> <ul class="section"> <li class="root"><span class="minus"><img src="images/minus.png"></span> antivirus compliance <ul class="mainlist" style="margin-left: -25px; display: block;"> <li class="desc"> <span style="color:#e4e4e4;">-</span> <span class="desc_span" tooltip="antivirus not installed.">not installed</span> </li> <li class="desc"> <span style="color:#e4e4e4;">-</span> <span class="desc_span" tooltip="antivirus not running.">not running</span> </li> <li class="desc"> <span style="color:#e4e4e4;">-</span> <span class="desc_span" tooltip="antivirus not updated.">not updated</span> </li> <li class="desc"> <span style="color:#e4e4e4;">-</span> <span class="desc_span" tooltip="">non compliant</span> </li> </ul> </li> </ul> <ul class="section"> <li class="root"><span class="minus"><img src="images/minus.png"></span> classification <ul class="mainlist" style="margin-left:-25px;"> <li class="desc"> <span style="color:#e4e4e4;">-</span> <span class="desc_span" tooltip="">classification</span> </li> <li class="desc"> <span style="color:#e4e4e4;">-</span> <span class="desc_span" tooltip="">dhcp</span> </li> <li class="desc"> <span style="color:#e4e4e4;">-</span> <span class="desc_span" tooltip="">p0f</span> </li> </ul> </li> </ul>
any appreciated. thanks!
you selecting span.minus img
ul
elements. need use $(this)
grab spans relative clicked ul
$("#xmldiv").on("click", "ul", function(e) { var clickedul = $(this); // reference clicked ul element clickedul.find(".mainlist").slidetoggle('slow', function () { // toggle .mainlist class element within clicked ul element clickedul.find('span.minus img'); // find img within clicked ul if(img.attr('src') === 'images/plus.png') img.attr('src', "images/minus.png"); else img.attr('src', "images/plus.png"); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="xmldiv"> <ul class="section"> <li class="root"><span class="minus"><img src="images/minus.png"></span> antivirus compliance <ul class="mainlist" style="margin-left: -25px; display: block;"> <li class="desc"> <span style="color:#e4e4e4;">-</span> <span class="desc_span" tooltip="antivirus not installed.">not installed</span> </li> <li class="desc"> <span style="color:#e4e4e4;">-</span> <span class="desc_span" tooltip="antivirus not running.">not running</span> </li> <li class="desc"> <span style="color:#e4e4e4;">-</span> <span class="desc_span" tooltip="antivirus not updated.">not updated</span> </li> <li class="desc"> <span style="color:#e4e4e4;">-</span> <span class="desc_span" tooltip="">non compliant</span> </li> </ul> </li> </ul> <ul class="section"> <li class="root"><span class="minus"><img src="images/minus.png"></span> classification <ul class="mainlist" style="margin-left:-25px;"> <li class="desc"> <span style="color:#e4e4e4;">-</span> <span class="desc_span" tooltip="">classification</span> </li> <li class="desc"> <span style="color:#e4e4e4;">-</span> <span class="desc_span" tooltip="">dhcp</span> </li> <li class="desc"> <span style="color:#e4e4e4;">-</span> <span class="desc_span" tooltip="">p0f</span> </li> </ul> </li> </ul>
Comments
Post a Comment