views:

48

answers:

2

Good Day,

I am working with jQuery tabs and I've got some code that fires off when I change tabs.

$('#container-1').tabs({ onClick: function(clickedTab, divElement, hiddenTab) {

    var selectedTab = clickedTab.toString();
// var pos = selectedTab.IndexOf("#") + 1;
var results = selectedTab.substring(5);

// selectedTab.IndexOf("#") + 1
alert(results);
    }
});

I've commented out the offending code, but when I try to determine the position of the # character, I get an error:

Object http://www.omnicom-innovations.com/play/tabsdemo1.html#fragment-2 has no method 'IndexOf'

I was under the imporession that by using the toString() method, it would convert the object to a string. This is based off of my understanding of a similar post:

http://stackoverflow.com/questions/3542843/jquery-and-split-not-working-together

If anyone can point out what's wrong, I'd greatly appreciate it.

TIA,

coson

+2  A: 

indexOf starts with a lower case I.

gawi
thanks, I can't believe that slipped by
coson
A: 

As far as I understood you are simply trying to get the value after #. To perform string operations in JavaScript, preferably you should use Regular Expressions because JavaScript is really fast interpreting them.

For this example you could do the following:

$('#container-1').tabs({ onClick: function(clickedTab, divElement, hiddenTab) {
        var selectedTab = clickedTab.toString();
        //Matches all characters after a #
        var results = /#.+/.exec(selectedTab);
        //var results will contain all matches of the used Expression as an Array, so you want to get the first result
        alert(results[0]);
    }
});

I hope this helps.

João Gala Louros