Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6969577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:35:56+00:00 2026-05-27T16:35:56+00:00

I have the following simple code: var tabs = null; function addTab(id, title, data)

  • 0

I have the following simple code:

var tabs = null;
function addTab(id, title, data) {
    alert("Begin: " + data.name);
    if (tabs === null) {
        tabs = $("#center-tabs").tabs({
            tabTemplate: "<li style='line-height: 0.5'><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close'>Remove Tab</span></li>",
            add: function (event, ui) {
                alert("Event: " + data.name);
            }
        });
    }
    tabs.tabs("add", id, title);
    tabs.tabs("select", id);
}

When I call:
addTab(“#tab-1”, “Tab-1”, {
name: “Example 1”
});
two alerts are displayed (“Begin: Example-1” and “Event: Example-1”)

But if I call again:
addTab(“#tab-2”, “Tab-2”, {
name: “Example-2”
});
two alerts are displayed but first one with correct message and second with the message of the first call (“Begin: Example-2” but “Event: Example-1”)

What I’m doing wrong?
Is it a tabs implementation bug?

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:35:57+00:00Added an answer on May 27, 2026 at 4:35 pm

    No, it is not a bug.

    First time you call addTab() variable tabs is null so the body of if-statement is executed. Inside the if you define add method with a body works with data.name (Example 1). This is expected.

    The problem is that during the 1st run a closure is created that contains the data variable with Example 1 value. When you call your addTab() 2nd time tabs is already created and inside the add method data.name is still referring to the old data.name which got saved during the 1st run. Your alert() cannot see the new value of the data.

    See http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/ .

    You can solve it by not accessing addTabs variables/parameters inside add directly because that will “save them” into a closure, but using other mechanism such as jQuery’s .data().

    var tabs = null;
    function addTab(id, title, data) {
        if (tabs === null) {
            tabs = $("#center-tabs").tabs({
                tabTemplate: "...",
                add: function (event, ui) {
                  console.log("Event: " + $(this).data('name'));
                }
            });
        }
        tabs.data('name', data.name);
        tabs.tabs("add", id, title);
        tabs.tabs("select", id);
    }
    

    HERE is an example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got the following very simple code: function init() { var articleTabs =
I have this following code: $(.preview).find(#panel).each(function(i, val) { var x = + $(val).html(); $(x).find(.dmPageHeadline).prepend(
I have the following quite simple test PHP code that extracts the data and
Let's assume that we have simple jQuery code like the following: var $document =
i have the following simple code, but it doesn,t work <ul> <li id=one onmouseover=this.style.background-color='white';>
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
I have the following code (correct for my simple tests) for a linked list
I have a simple DOM code like the following <div> <div> </div> </div> I
I have used following code to create a simple PDF file. It executes fine
I have the following code that fills dataTable1 and dataTable2 with two simple SQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.