Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass this element to javascript onclick function and add a class to that clicked element

I had an html navigation code as below

function Data(string) {   //1. get some data from server according to month year etc.,   //2. unactive all the remaining li's and make the current clicked element active by adding "active" class to the element   $('.filter').removeClass('active');   $(this).addClass('active'); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="row" style="padding-left:21px;">   <ul class="nav nav-tabs" style="padding-left:40px;">     <li class="active filter"><a href="#month" onclick="Data('month')">This Month</a></li>     <li class="filter"><a href="#year" onclick="Data('year')">Year</a></li>     <li class="filter"><a href="#last60" onclick="Data('last60')">60 Days</a></li>     <li class="filter"><a href="#last90" onclick="Data('last90')">90 Days</a></li>   </ul> </div>

When the user clicks on any of the tabs

  • all the remaining tabs should be unactive,
  • and the current element/tab should be active,

My code above is not working.

  1. How to make the above code work?
  2. I only want to use javascript onclick for this. Is there any way that the this(current) object is send when the user clicks on the tab?
like image 524
Shiva Krishna Bavandla Avatar asked Nov 15 '13 10:11

Shiva Krishna Bavandla


People also ask

How do I add a class to Onclick?

To add a class on click of anchor tag, we use addClass() method. The addClass() method is used to add more property to each selected element. It can also be used to change the property of the selected element.

How do you pass this function in JavaScript?

You can use addEventListener to pass this to a JavaScript function.


2 Answers

Use this html to get the clicked element:

<div class="row" style="padding-left:21px;">     <ul class="nav nav-tabs" style="padding-left:40px;">         <li class="active filter"><a href="#month" onclick="Data('month', this)">This Month</a></li>         <li class="filter"><a href="#year" onclick="Data('year', this)">Year</a></li>         <li class="filter"><a href="#last60" onclick="Data('last60', this)">60 Days</a></li>         <li class="filter"><a href="#last90" onclick="Data('last90', this)">90 Days</a></li>     </ul>  </div> 

Script:

 function Data(string, el)  {      $('.filter').removeClass('active');      $(el).parent().addClass('active');  }  
like image 74
Sujata Chanda Avatar answered Sep 30 '22 17:09

Sujata Chanda


You can use addEventListener to pass this to a JavaScript function.

HTML

<button id="button">Year</button> 

JavaScript

(function () {     var btn = document.getElementById('button');     btn.addEventListener('click', function () {         Date('#year');     }, false); })();   function Data(string)           {                 $('.filter').removeClass('active');                 $(this).parent().addClass('active') ;           }  
like image 39
Naman Avatar answered Sep 30 '22 17:09

Naman