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

The Archive Base Latest Questions

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

I have a problem with addEvent(or something else I don’t know). This is my

  • 0

I have a problem with addEvent(or something else I don’t know). This is my code.
HTML :

<div style="background-color:red;height:30px;width:30px;" ></div>
<div style="background-color:yellow;height:30px;width:30px;" ></div>

JS :

function addEvent(elm,ev,fun)//Cross-browser addEvent
{
    if(elm.addEventListener)elm.addEventListener(ev,fun,false);
    else if(elm.attachEvent)
    {
        var r=elm.attachEvent("on"+ev,fun);
        return r;
    }
}

function myFun(x)
{
    alert(x.style.backgroundColor);
}

var allDiv=document.getElementsByTagName("div");
myFun(allDiv[0]);//Here it works
for(var i=0;i<allDiv.length;i++) {
      //But not here when you click the div
      addEvent(allDiv[i],
               "click",function(){
                          myFun(allDiv[i])
                       });
}

I want that the alert works but without using IDs and without puting an inline onclick. Is it possible ?

http://jsfiddle.net/G9gBS/1/

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

    You’ve fall into a common trap when using closures in JavaScript. I is closed over but changes on all click functions are calling myFunc with allDiv[allDiv.length -1] you can use a self executing function to get around this

    for(var i=0;i<allDiv.length;i++) {
              addEvent(allDiv[i],
                       "click",(function(j) {
                                  return function(){
                                           myFun(allDiv[j])
                                         }
                                }(i)));
    }
    

    this no longer closes over i (i is never referenced in the function assigned to the click event).

    in this particualr case however you can simply pass this. An event handler is called in the context of the element firing the event so you can do

    for(var i=0;i<allDiv.length;i++) {
                  addEvent(allDiv[i],
                           "click",function(){myFun(this)});
    }
    

    or even shorter using jQuery

    var myFun function(x)
    {
        alert(x.style.backgroundColor);
    }
    
    $("div").click(myFun);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with my code. I don't know what am I doing wrong.
I have problem with this code..I want to extract data from flat file and
I have a problem with the code below. I can't get it to popup
I have this form: <div class=row border-tb> <form id=test-form action=/test/insert method=post class=form-inline offset4 span16>
I have a problem with the Mootools get method and IE8. This is the
I have some problem with a touchend event on iOS: var target; window.addEvent('domready', function(){
I'm new to Mootools and I'm stuck on the following problem. I have this
I have the following backbone.js code and i have a problem in that event
I have asked this question before and it seemed that the code I was
I have problem understanding this one: <?php $string = 'April 15, 2003'; $pattern =

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.