Martin Ongtangco
Martin Ongtangco

Reputation: 23505

how to get tab index by tab name in jquery?

how to get tab index by tab name in jquery?

i need to remove a certain tab by this command:

$(tabContainer).tabs('remove', index);

the index must contain the correct order of the tab to be closed. the problem is, i'm generating the tabs programmatically so chances of having the wrong index is likely.

Upvotes: 3

Views: 5075

Answers (3)

Julien Ribon
Julien Ribon

Reputation: 275

I think this is what you want ("name" is the name of your tab) :

// close tab with a given name
function removeTab(name) {

    var tab = $('#tabs a').filter(function(){
        return $(this).text() == name;
    }).parent();

    var index = $( "li", $tabs ).index(tab);
    if (index>=0) {
        $tabs.tabs( "remove", index );
    }
}

Upvotes: 3

Curtis
Curtis

Reputation: 391

What I ended up doing is just looping through the list elements and looking for the text. May not be the most efficient way but it works:

var i = 0;
$('#yayTabs ul li').each(function() {
    if($(this).children().text() === "TabText") {
        $('#yayTabs').tabs("remove", i);
        return false; //break out of $.each loop;
    }
    i++;
});

Upvotes: 0

Jojo
Jojo

Reputation: 348

You may need to give an example of your HTML & JS/jQuery but here is what I think you need.

$('ul li a').live('click', function(){ 
    var index = $(this).parent().index($(this).parent());
    alert(index); 
});

Upvotes: 0

Related Questions