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 3674204
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:49:36+00:00 2026-05-19T02:49:36+00:00

I’m trying to do a custom tabs system using JavaScript. However, in order to

  • 0

I’m trying to do a custom tabs system using JavaScript. However, in order to reuse the code I want to write it in OOP style. This what I have so far:

function Tabs()
{

    this.tabArray = new Array(arguments.length);
    this.tabArrayC = new Array(arguments.length);
    
    for(i=0;i<arguments.length;i++)
    {
        this.tabArray[i] = arguments[i];
        this.tabArrayC[i] = arguments[i]+'_content';
    }
    
    this.setClickable = setClickable;

    function setClickable()
    {   

        for(i=0;i<this.tabArray.length;i++)
        {
                        
                document.getElementById(this.tabArray[i]).onclick = function()
                {
                    
                    alert(this.tabArray[i]);
                                    
                }
            
        }
                
    }
    
}

function init()
{
    tab = new Tabs('tab1','tab2','tab3','tab4');
    tab.setClickable();
}

window.onload = init();

Now here’s the deal. I want to assign the onclick event handler to every tab that has been passed in Tabs ‘class’ constructor. So later in the code when I write something like:

<div id="tab1">Tab1</div>
<div id="tab2">Tab2</div>
<div id="tab3">Tab3</div>
<div id="tab4">Tab4</div>

The code which has been set up earlier:

document.getElementById(this.tabArray[i]).onclick = function()
{
                    
                    alert(this.tabArray[i]);
                                    
}

… would be executed. I hope I explained that well enough. Any ideas?

  • 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-19T02:49:37+00:00Added an answer on May 19, 2026 at 2:49 am

    There are three issues with your setClickable function (edit: and an issue with how you’re calling init):

    1. this will have a different meaning within the event handler you’re generating than you expect. (More here: You must remember this)

    2. A closure (a function that closes over data like your i variable) has an enduring reference to the variable, not a copy of its value. So all of the handlers will see i as of when they run, not as of when they’re created. (More here: Closures are not complicated)

    3. You’re not declaring i, and so you’re falling prey to the Horror of Implicit Globals.

    Here’s one way you can address those:

    function setClickable()
    {   
        var i;           // <== Declare `i`
        var self = this; // <== Create a local variable for the `this` value
    
        for(i=0;i<this.tabArray.length;i++)
        {
                                                             // v=== Use a function to construct the handler
            document.getElementById(this.tabArray[i]).onclick = createHandler(i);
        }
    
        function createHandler(index)
        {
            // This uses `self` from the outer function, which is the
            // value `this` had, and `index` from the call to this
            // function. The handler we're returning will always use
            // the `index` that was passed into `createHandler` on the
            // call that created the handler, so it's not going to change.
            return function() {
                alert(self.tabArray[index]);
            };
        }
    }
    

    …and as goreSplatter and Felix point out, this line:

    window.onload = init();
    

    …calls the init function and uses its return value to assign to onload. You mean:

    window.onload = init;
    

    …which just assigns init to the onload event.


    Off-topic: You might consider using the newer “DOM2” mechanisms for attaching event handlers instead of the old “DOM0” way of using the onXYZ properties and attributes. The new way is called addEventListener, although sadly Internet Explorer has only recently added that (but it has attachEvent which is very similar). If you use a library like jQuery, Prototype, YUI, Closure, or any of several others, they’ll smooth out those differences for you (and provide lots of other helpful stuff).

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

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.