Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery check if tab with a particular title already exists

I would like to get a code snippet which demonstrates how to check if the tab is already opened.

I need the following functionality: 1.user click a button to add additonal tab 2.check if a tab with these title already exists 3.select existing tab if already exists otherwise add a new tab

Best regards, Javanus

like image 544
javanus Avatar asked Feb 24 '11 20:02

javanus


2 Answers

Something like this:

var nameToCheck = "SomeNewTabName";
var tabNameExists = false;

$('#tabs ul li a').each(function(i) {
    if (this.text == nameToCheck) {
        tabNameExists = true;
    }
});

if (!tabNameExist){
    //code to insert new tab here
}

I'm going with the assumption that you are using jQuery UI tabs here...

like image 139
Dubmun Avatar answered Nov 10 '22 10:11

Dubmun


Your solution to select the existing tab is a bit complex. The following line, to be placed just below tabNameExists = true;, works fine and is very simple

$( "#tabs" ).tabs('option', 'active', i);
like image 21
Pierre Avatar answered Nov 10 '22 08:11

Pierre