Reputation: 1099
I want to use .addClass to give my li element a class based on the url of the a element. How do I achieve this with jQuery?
<li><a href="/m4n?seid=etailer-orderhistorylist">Tidigare ordrar</a></li>
Upvotes: 0
Views: 44
Reputation: 3760
If i understood your question correctly, you want to put <a>s
content(text) as a class of <li>
and if thats the case.
$("a").each(function(){
$(this).parent().addClass($(this).text());
});
Upvotes: 0
Reputation: 20740
Try like below. Hope this will help you.
$('li > a').each(function () {
if (this.href == '/m4n?seid=etailer-orderhistorylist') {
$(this).parent().addClass('your_class');
}
});
Upvotes: 0
Reputation: 34199
Just specify this href in a selector and use jQuery parent()
and addClass()
:
$("a[href='/m4n?seid=etailer-orderhistorylist']").parent().addClass('myClass');
Upvotes: 1