css - JQuery: adding class to element if it has text -
I have a series of links in ul, some have text and nothing else, eg:
& lt; Ul & gt; & Lt; Li & gt; & Lt; A href = "google.com" & gt; Google & lt; / A & gt; & Lt; / Li & gt; & Lt; Li & gt; & Lt; A href = "yahoo.com" & gt; Yahoo & lt; / A & gt; & Lt; / Li & gt; & Lt; Li & gt; & Lt; A href = "bing.com" & gt; Bing & lt; / A & gt; & Lt; / Li & gt; & Lt; Li & gt; & Lt; A href = "#" & gt; & Lt; / A & gt; & Lt; / Li & gt; & Lt; Li & gt; & Lt; A href = "#" & gt; & Lt; / A & gt; & Lt; / Li & gt; & Lt; Li & gt; & Lt; A href = "#" & gt; & Lt; / A & gt; & Lt; / Li & gt; & Lt; / Ul & gt; I just want to use JQuery to apply a tag to that category in which it has text.
So far I have this:
var buttontext = $ ('ul a'). Text (); If (buttontext.length & gt; 0) {$ ('ul A'). AddClass ('Buttonfet'); } But the class is still affecting all the tags. I was just wondering if anyone could tell me in the right direction?
Thanks
$ ("ul a"). Each (function (i, e) {var $ e = $ (e); if ($ e.text (). Length> 0) {$ E.addClass ("buttonfetch");}});
Comments
Post a Comment