javascript - Closest function in jquery and extracting the elements -
i have html fragment follows:
<div id="samplediv"> <ul> <li name="a"> <a id="a"> </li> <li name="b"> <a id="b"> </li> </ul> </div>
i have text called:
var text = "b";
i have want check if text matches of elements of li , add class name "disable" anchor element not matching text. case want add class called "disable"
<a id="a">
this have tried:
$("#samplediv li").each(function() { if($(this).name != text){ $(this).closest("a").addclass("disabled"); } });
but thing here $(this).name
evaluating "undefined"
. missing?
edit: due typo ,had missed tag
there multiple issues,
$(this)
returns jquery object not havename
property, instead can use$(this).attr('name')
.closest()
used find ancestor element,a
descendant ofli
element, need usefind()
you can find li
elements not have given name , find a
element within like
var text = 'b'; $("#samplediv li").not('[name="' + text + '"]').find("a").addclass("disabled");
a.disabled { color: green; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="samplediv"> <ul> <li name="a"> <a id="a">a</a> </li> <li name="b"> <a id="b">b</a> </li> </ul> </div>
Comments
Post a Comment