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

  • Home
  • SEARCH
  • 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 6362087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:51:45+00:00 2026-05-24T23:51:45+00:00

I am trying to make an website. I am putting addEventListener to more elements

  • 0

I am trying to make an website. I am putting addEventListener to more elements in a function called more times:

    idImag = 0;

    function function1()
    {
        //do something
        function2()
    }

    function function2()
    {
        //do something
        document.getElementById("holder" + idImag).addEventListener('mouseover',function(){
         idImag++;
alert('It works');
    }

    function3(event)
    {
        alert(3);
    }

    function1();
    function1();

<div id="holder0">Dog</div>
<div id="holder1">Chicken</div>
<div id="holder2">Cow</div>

But there is a problem: Only the last element gets the event listener… the others do nothing after putting the mouse over it.
Then I’ve googled a little and found out about closures and how variables are kept even after function returned… I didn’t understand everything, but I just want to find out how to put the event listeners in function2. Can you help me?
Probably you noticed: I am a newbie. Sorry if the question is stupid or if it has no sense. If you need more details, I will put my whole code, but it has variables and comments in Romanian, so I am not sure if you will understand it. Sorry for my bad English and thank you 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-24T23:51:46+00:00Added an answer on May 24, 2026 at 11:51 pm

    Why not a for loop?

    function function3 (event) {
        alert(3);
    }
    
    for (var idImag = 0; i< numberOfHolders; i++) {
        //do something1
        //do something2
        document.getElementById("holder" + idImag).addEventListener('mouseover',function3);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a website more dinamically with splitting it in parts. I
Hey i'm trying to make my website delete a file using the unlink(); function.
i'm trying to make my second website using php and i'm stuck at some
I'm trying to make a navigation for my website and would greatly appreciate any
I'm trying to make a gradient background for my website, http://www.lathamcity.com The problem is,
I'm trying to make a Google Maps widget for my website but all of
Hello i'm trying to make a tag script for my website so each time
I am trying to make the the ads at the bottom of the website
I'm trying to make a clean url for a blog on a dynamic website,
I'm trying to make a nice-looking CSS menu, for this website . (The domain

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.