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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:42:41+00:00 2026-05-31T08:42:41+00:00

I’m running into an issue with the addEventListener() function. I’m trying to build a

  • 0

I’m running into an issue with the addEventListener() function. I’m trying to build a menu based on variables from an array (I won’t bother explaining why it’s setup this way).

Here is the code:

var _Dirt = document.getElementById("dirt");

_Dirt.addEventListener('mouseover', function () { menuOver(true, this, _dirtY); }, false);
_Dirt.addEventListener('mouseout', function () { menuOver(false, this, _dirtY); }, true);

var _DirtDiv = _Dirt.getElementsByTagName('div')[0];

for (nLink in Men["dirt"]) {

    var _lnk = document.createElement('p');
    _lnk.className = 'menu';
    _lnk.innerHTML = Men["dirt"][nLink][0];
    _lnk.addEventListener('click', menuLoop(Men["dirt"][nLink][1]), false);

    _DirtDiv.appendChild(_lnk);
    _DirtDiv.innerHTML+= '<br />';

}

Here is the menuLoop function (in case you are concerned that I’m not correctly binding the variable right:

function menuLoop (n) {

    return function () {

        menuLink (n);

    }

}

Now, the annoying part is that the event listener is working fine on the _Dirt element, but not at all on the _lnk element. Even when I change it to to _lnk.onclick = function () { //something }, it won’t work (as a matter of fact, the onclick attribute won’t even show up in the DOM).

I have another loop function that also attaches an event listener to an element, and that one works perfectly fine…and the code is very similar. What adds to the frustration is that this code used to work flawlessly. I actually don’t even recall making any changes to this code at all. I tried to isolate this function in the code to see if there were any breakpoints in my script, but it the issue remains.

Please let me know if you require any other parts of my code.

  • 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-31T08:42:42+00:00Added an answer on May 31, 2026 at 8:42 am

    (This is under the assumption your data structures are all correct)

    Your problem is

    _DirtDiv.innerHTML += '<br />';
    

    This basically takes the child nodes of _DirtDiv, serializes them to a string (HTML), concatenates '<br />' and assigns it back to innerHTML. The browser will parse the string and create the corresponding DOM nodes.

    The identity of the DOM node you created before is lost and so is the event handler. This process destroys and creates new DOM nodes which are different than the one you created before.

    If you want to add a br node, use DOM manipulation as well:

    _DirtDiv.appendChild(document.createElement('br'));
    

    Example from the Chrome console which demonstrates that the nodes are different:

    > var node = document.createElement('div');
      undefined
    > node.id = 'foo'; 
      "foo"
    > document.body.appendChild(node);
      <div id=​"foo">​</div>​
    > node === document.getElementById('foo');
      true                                       // nodes are the same
    > document.body.innerHTML += '<br />';
      "... long HTML string ..."
    > node === document.getElementById('foo');
      false                                      // nodes are different
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
For some reason, after submitting a string like this Jack’s Spindle from a text
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
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:

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.