Jerry
Jerry

Reputation: 1099

Add class to parent based on childelements link url

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

Answers (3)

Hemal
Hemal

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

Ibrahim Khan
Ibrahim Khan

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

Yeldar Kurmangaliyev
Yeldar Kurmangaliyev

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

Related Questions