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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:11:52+00:00 2026-05-26T19:11:52+00:00

I´m trying to make a loop inside an array, and add events to its

  • 0

I´m trying to make a loop inside an array, and add events to its elements, not succeeding.
This is the array:

div_empresa_links_elements.push({
    div_mouseover: div_imagens_mouseover,
    div_submenu: div_empresa_imagens,
    div_txt: div_imagens_txt
}, {
    div_mouseover: div_institucional_mouseover,
    div_submenu: div_empresa_institucional,
    div_txt: div_institucional_txt
}, {
    div_mouseover: div_localizacao_mouseover,
    div_submenu: div_empresa_localizacao,
    div_txt: div_localizacao_txt
}, {
    div_mouseover: div_infraestrutura_mouseover,
    div_submenu: div_empresa_infraestrutura,
    div_txt: div_infraestrutura_txt
}, {
    div_mouseover: div_politicaDeQualidade_mouseover,
    div_submenu: div_empresa_politicaDeQualidade,
    div_txt: div_politicaDeQualidade_txt
}, {
    div_mouseover: div_linhaDoTempo_mouseover,
    div_submenu: div_empresa_linhaDoTempo,
    div_txt: div_linhaDoTempo_txt
}, {
    div_mouseover: div_historico_mouseover,
    div_submenu: div_empresa_historico,
    div_txt: div_historico_txt
})

This is the loop:

for (var i = 0; i < div_empresa_links_elements.length; i++) {
    var obj = div_empresa_links_elements[i]
    obj.div_mouseover.onmouseover = function () {
        obj.div_txt.style.opacity = 1
        obj.div_txt.style.filter = 'alpha(opacity = 100)'
        obj.div_submenu.style.opacity = .7
        obj.div_submenu.style.filter = 'alpha(opacity = 70)'
    }
}

The thing just works, if I put an event for each of the elements (_mouseover), by hand without a loop. Can anyone tell me why?

  • 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-26T19:11:53+00:00Added an answer on May 26, 2026 at 7:11 pm

    The issue is caused by a closure issue. Currently, obj is overwritten at each iteration. So, obj points to the lastly defined element. To fix this, wrap the the loop’s body in a self-invoking function, and pass the variable obj as an argument, as shown below:

     for (var i = 0; i<div_empresa_links_elements.length;i++){
        var obj = div_empresa_links_elements[i]
        (function(obj){ //obj is declared again. Inside this func, obj = <see below>
            obj.div_mouseover.onmouseover = function(){
                obj.div_txt.style.opacity = 1
                obj.div_txt.style.filter = 'alpha(opacity = 100)'
                obj.div_submenu.style.opacity = .7
                obj.div_submenu.style.filter = 'alpha(opacity = 70)'
            }
        })(obj); //Pass the variable obj as defined at the top
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to make a make generic select control that I can dynamically add elements
I'm trying to make a function array, which I'm using inside a List.reduce HOF.
I'm trying to make a loop that will recurse through an array of bytes
I am trying to make a stop button for this loop but it runs
I am trying to see if I can make my own simulation loop inside
After trying to make a while(bool) loop and it failing because I couldn't see
I am trying to make a loop that loops through all different integers where
I'm trying to make a loop to check if certain files exist, and if
I am trying to make a sequential animation, where a loop iterates through a
I am trying to make a loop to redo a Matlab function 1000 times.

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.