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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:13:50+00:00 2026-06-03T08:13:50+00:00

So I have a problem where the eventlisteners I setup all happen to work

  • 0

So I have a problem where the eventlisteners I setup all happen to work with the same variable.
This is how it looks like:

// Prepare tooltips
for (var i = 0; i < document.getElementsByClassName("tooltip").length; i++) {

    var tooltip = document.getElementsByClassName("tooltip")[i];
    var input = document.getElementsByName(tooltip.id.substr(8))[0];

    var offsetTop = 0;
    var tmp = input;

    while (tmp != null) {
        offsetTop += tmp.offsetTop;
        tmp = tmp.offsetParent;
    }

    offsetTop -= 130;

    var offsetLeft = (input.offsetParent.offsetLeft + input.scrollWidth) + 50;

    tooltip.innerHTML += "<div class='corner'></div>";
    tooltip.style.top = offsetTop + "px";
    tooltip.style.left = offsetLeft + "px";

    input.addEventListener("focus", function() { document.getElementById(tooltip.id).style.display = "block"; });
    input.addEventListener("blur", function() { document.getElementById(tooltip.id).style.display = "none"; });
}

In the last two lines I set the eventlisteners.
So whenever I focus an input field, no matter which one tooltip.id is always the same.
I checked the input.id before its different in every loop.

  • 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-06-03T08:13:52+00:00Added an answer on June 3, 2026 at 8:13 am

    Javascript is a funny language 🙂
    In each loop you’re declaring a function which uses a reference to the variable tooltip.
    Since you use the variable many times: its value changes but the reference remains the same.
    When the function executes, it uses the reference (which has the last value).

    Here is the solution:
    (I recommend calling the method ‘document.getElementsByClassName(“tooltip”)’ only once since it causes DOM traverse.

    ==== CODE STARTS HERE

    var toolips = document.getElementsByClassName(“tooltip”);
    for (var i = 0; i < toolips.length; i++)
    {
    var tooltip = toolips[i];
    var input = document.getElementsByName(tooltip.id.substr(8))[0];

    var offsetTop = 0;
    var tmp = input;
    
    while (tmp != null)
    {
        offsetTop += tmp.offsetTop;
        tmp = tmp.offsetParent;
    }
    
    offsetTop -= 130;
    
    var offsetLeft = (input.offsetParent.offsetLeft + input.scrollWidth) + 50;
    
    tooltip.innerHTML += "<div class='corner'></div>";
    tooltip.style.top = offsetTop + "px";
    tooltip.style.left = offsetLeft + "px";
    
    
    // assign tooltip id to the input
    input.tooltipId = tooltip.id;
    
    // add event listeners
    input.addEventListener("focus", function() { document.getElementById(this.tooltipId ).style.display = "block"; });
    input.addEventListener("blur", function() { document.getElementById(this.tooltipId).style.display = "none"; });
    

    }

    ==== CODE ENDS HERE

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

Sidebar

Related Questions

i have problem with autorotate on iphone i set up in all classes -
I have problem with cakephp's Session->write method. If I set a value like $_SESSION['..']
somewhat simple problem(to explain) this time: i have an array of markers that i
If i have something like this, where i can control the auto-scrolling using boolean
I have a problem with eventlisteners in javascript that is probably due to my
I really have tried my hardest on this little problem, its been 2 weeks
I need some help! I have no doubt this is a dumb problem that
I have created the following code http://jsfiddle.net/N65yS/41/ .. My problem is this.. When I
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I have problem with fancybox. I want to write a function that will run

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.