Fizi
Fizi

Reputation: 1851

get an html element from the title of a child element

I have list that has one active class element

<ul class="tabs clearfix" >
<li>  
  <a title="abc" href=# >ABC</a> 
</li>
<li>
  <a title="xyx" href=# >XYZ</a>
</li>
<li class=active > 
  <a title="all" href=# >All</a> 
</li>

I am using local storage to store the title of a tag which is then used to loadcontent accordingly. I am having difficulty in getting the specific li element when i fetch the titel back from local storage. I would like to add an active class to li element that corresponds to the specific title. Here's the javascript code:

$(document).ready(function(){
var tabs = $('.tabs > li');
if (localStorage.getItem("tabIndex") === null) {
    $( "#content" ).load( "{{url_for('mypage', query=query)}}" );
}else{
    var lastIndex = localStorage.getItem('tabIndex');
    **Do something to get the specific li element for the title**
    **addclass('active') for the li**
    LoadContent(lastIndex);
} 
tabs.on("click", function(){
    tabs.removeClass('active');
    localStorage.setItem('tabIndex', $('a', this).attr('title'));
    $(this).addClass('active');
    var index = $(this).children('a')[0].title;
    LoadContent(index);
})
});

I tried $("a[title=lastIndex]").parent().addClass('active'); but this doesnt seem to work. As always, I really appreciate all the help that this site provides me. Thanks

Upvotes: 4

Views: 111

Answers (1)

Joy Biswas
Joy Biswas

Reputation: 6527

You can use $('a[title="'+ lastIndex +'"]') to get the desired element,

Eg: say your lastIndex = xyz then the selector becomes $('a[title="xyz"]')

Upvotes: 4

Related Questions