Bahramdun Adil
Bahramdun Adil

Reputation: 6079

I am unable to get the parent element of the anchor tag

HTML code:

<ul class="nav nav-tabs">
    <li role="presentation" id="tab1" class="active"><a onclick="switchTabs(1)">Tab1</a></li>
    <li role="presentation" id="tab2"><a onclick="switchTabs(2)">Tab2</a></li>
</ul>

JS code: no error occurred while clicking, but not do switching

function switchTabs(idx) {
    // this is not working
    var $li = $(this).parent();
    if($li.hasClass('active')) {
        return false; // not enter here
    }
    $li.removeClass('active');
    if (idx === 1) {
        $li.next().addClass('active');
    } else {
        $li.prev().addClass('active');
    }

    // but this works while using id selector
    /*if (idx === 1) {
        $("#tab1").addClass('active');
        $("#tab2").removeClass('active');
    } else if(idx === 2) {
        $("#tab2").addClass('active');
        $("#tab1").removeClass('active');
    }*/
    return false;
}

What can be the reason?

Upvotes: 1

Views: 124

Answers (4)

Bhushan Kawadkar
Bhushan Kawadkar

Reputation: 28513

You are passing only index and not this object to switchTab function. You can make use of idx to get the clicked li and then put logic to add / remove active class.

See code below

function switchTabs(idx) {
    // this is not working
    //var $li = $(this).parent(); -- you are not passing this anywhere in the function hence it is not getting
    $('li').removeClass('active'); // remove all active 
    var $li = $('#tab' + idx).addClass('active'); // use idx to create tab id of clicked li and add active class
}
.active {
  color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<ul class="nav nav-tabs">
    <li role="presentation" id="tab1" class="active"><a onclick="switchTabs(1)">Tab1</a></li>
    <li role="presentation" id="tab2"><a onclick="switchTabs(2)">Tab2</a></li>
</ul>

Upvotes: 0

manelseo
manelseo

Reputation: 597

Try this approach I think that will be better solution for your case

<html>
<head>
    <meta charset="utf-8"/>
</head>

<style>
    .active {
        color: red;
    }
</style>

<body>
<ul class="nav nav-tabs">
    <li role="presentation" id="tab1" class="active"><a onclick="switchTabs(this)">Tab1</a></li>
    <li role="presentation" id="tab2"><a onclick="switchTabs(this)">Tab2</a></li>
</ul>
</body>

<script
              src="https://code.jquery.com/jquery-3.4.1.js"
              integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
              crossorigin="anonymous"></script>

<script>

function switchTabs(elm) {
    var $li = $(elm).parent();
    if(!$li.hasClass('active')) {
        $li.addClass('active');
        $li.siblings().removeClass('active');
    }
}

</script>

Upvotes: 3

Roy Bogado
Roy Bogado

Reputation: 4452

Check this method to make it easier (and use the $(this))

$(document).on('click','.nav li a', function(){                                      
  let tab = $(this).data('tab');//$(this) is the a clicked.                                      
  $('.nav li.active').removeClass('active');//hide active Tab
  $('.nav li#tab'+tab).addClass('active');//show Tab
})
.active{font-size:20px;color:red}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="nav nav-tabs">
    <li role="presentation" id="tab1" class="active"><a data-tab="1">Tab1</a></li>
    <li role="presentation" id="tab2"><a data-tab="2">Tab2</a></li>
</ul>

Upvotes: 0

Gugu
Gugu

Reputation: 193

Send the element into the function, and then you will have it's parent:

<a onclick="switchTabs(this,1)">

and then

function switchTabs(elm,idx) {
// this is not working
  var $li = $(elm).parent();
  // Rest of your code...

Upvotes: 2

Related Questions