0

I have a search box with two tabs to choose which type to search. I want to add .active class to list item whose search_type equals to a params from a request (i.e., params[request_type]).

<ul data-tabs="tabs" class="tabs">
  <li search_type="Things">
     <a href="#Things">Things</a>
  </li>
  <li search_type="People">
    <a href="#People">People</a>
  </li>  
</ul>

I tried to compare in jQuery:

$('.tabs .li).(function(){  
if($(this).search_type == myParams){
   $(this).addClass('.active');
  }
});

However, my $(this).search_type turns out to be undefined, even though in web-browser, the element shows up correctly as:

<li search_type="Things"> 
  <a href="#Things">Things</a>
</li>

My question is: how to I get search_type from li?

Thank you.

3 Answers 3

4
$('.tabs .li').(function(){  
if($(this).attr('search_type') == myParams){
   $(this).addClass('active');
  }
});

Notice the $(this).attr() and also, in addClass dont put . ... only the class name.

Sign up to request clarification or add additional context in comments.

Comments

2

It should be $(this).attr('search_type').

Comments

2

You cannot reference search_type directly, try $(this).attr('search_type') and see what happened

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.