Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing A JQuery UI Tab

I am attempting to use some (simple) code to remove a JQuery UI tab that I had created dynamically. Unfortunately, it's not cooperating.

Here is the javascript method I'm using:

    function removeTab(tabName) {

        var tabIndex = -1;
        $('#tabbedPanel > ul > li > a').each(
                function(i) {
                    if (this.href == tabName) {
                        tabIndex = i;
                    }
                });

        $('#tabbedPanel').tabs("remove", tabIndex);

    }

Unfortunately, all I get is "Object doesn't support this property or method". I'm sure that the tab index is correct.

Any help out there?

like image 421
Jason Avatar asked Feb 06 '26 15:02

Jason


1 Answers

Glad you got it working.

One suggestion - instead of looping through the tabs, you can use the tabs API to figure out the selected index of the tab and remove it a lot easier.

Here's a fiddle I put together (using the jQueryUI demo tabs with some enhancements).

The key part is using the 'selected' option. I have a button that, when clicked, will alert the selected tab index...

$("#get_index").click(function(e) {
    e.preventDefault();
    alert("Selected tab index = " + $("#tabs").tabs("option", "selected"));
});

I have a different button that, when clicked, will get the selected index and then use the 'remove' option to remove that tab. This way you don't have to match the href name...

$("#remove_selected").click(function(e) {
    e.preventDefault();
    var selIndex = $("#tabs").tabs("option", "selected");
    $("#tabs").tabs("remove", selIndex);
});

Even though you solved your problem, I thought I'd share this. Hope it helps!!

like image 97
David Hoerster Avatar answered Feb 09 '26 11:02

David Hoerster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!