We have:
<div class="companylinks">
<span class="current">
<a class="welcome">Welcome!</a>
</span>
<span>
<a class="ic_generalinfo">General Info</a>
</span>
</div>
I want to get the class of the <a> which is in the <span class="current"> (here is ‘welcome’)
var selected_tab = $('#companylinks span[class="current"]').closest('a').attr('class');
alert(selected_tab);
But I’m getting ‘undefined’, how I should ise closest() here?
I would appreciate any help.
Thanks
.closest()goes up the parent chain, not down the parent chain.To specify multiple parents that all must be satisfied in a selector, you can just list them separately in the selector separated by spaces. In this case you can target the
<a>tag inside of .current and then obtain its class like this:Or, if you want to scope it only to inside of
.companylinks, then you can use this;Note: You don’t need to use
span[class=".current"]. You can just use.current.