Javascript: event does not fire if element is not attached to document.body

I dynamically create an element (div) in javascript on which I register an event listener:

var tooltip = document.createElement('div');
tooltip.onclick = function() { alert('hello'); } 

      

Now if I attach this element to the document body:

document.body.appendChild(tooltip);

      

all is well and the event is captured. However (for positioning purposes) I want to attach this element to a (static) sub-element on my page, like so:

document.getElementById('id').appendChild(tooltip);

      

and the element is created and positioned correctly, but now the onclick event is no longer caught. Any thoughts? This is an x ​​browser, so I must be missing something.

Thanks Don.

0


source to share


5 answers


Perhaps you need to register an event handler after adding it?



+1


source


You are creating not only one, but MANY div. Try this instead (I hope you don't mind, but I've corrected the HTML and CSS too):



<html>
<head>

<script type="text/javascript">

function makeDiv() {
    if(!document.getElementById('tooltipDiv')){
        var tooltip = document.createElement('div');

        tooltip.id = "tooltipDiv";
        // Give our tooltip a size and colour so we can see it
        tooltip.style.height = '200px';
        tooltip.style.position = 'absolute';
        tooltip.style.width = '200px';
        tooltip.style.backgroundColor = '#eee';

        // Register onclick listener
        tooltip.onclick = function() { alert('hello'); }
        //tooltip.addEventListener("click", function(){ alert('hello'); }, false);

        // *** Comment one of these out: ***

        //document.body.appendChild(tooltip);
        document.getElementById('myDiv').appendChild(tooltip);
    }
}

</script>
</head>

<body>


<div id="myDiv" 
     onmouseover="makeDiv();" 
     style="position: relative; top: 100px; left: 100px; border: 1px solid red; width: 200px;">

<span>my div text</span>

</div>

</body>
</html>

      

+1


source


Your code works fine for me in firefox 3.0.5 and IE7. Are you sure your example is correct?

0


source


Ok, here is my code, sorry for the delay. Below is a version with a workaround:

<html>
<head>

<script type="text/javascript">

function makeDiv() {
  var tooltip = document.createElement('div');

  // Give our tooltip a size and colour so we can see it
  tooltip.style.height = '200px';
  tooltip.style.position = 'absolute';
  tooltip.style.width = '200px';
  tooltip.style.backgroundColor = '#eee';

  // Register onclick listener
  tooltip.onclick = function() { alert('hello'); }

  // *** Comment one of these out: ***

  //document.body.appendChild(tooltip);
  document.getElementById('myDiv').appendChild(tooltip);
}

</script>
</head>

<body>


<div id="myDiv" 
     onmouseover="makeDiv();" 
     style="position: relative; top: 100px; left; 100px; border: 1px solid red; width: 200px;">

<span>my div text</span>

</div>

</body>
</html>

      

=================================== OK - this is how it works:

<html>
<head>

<script type="text/javascript">

function makeDiv() {
  var tooltip = document.createElement('div');

  // Give our tooltip a size and colour so we can see it
  tooltip.style.height = '200px';
  tooltip.style.position = 'absolute';
  tooltip.style.width = '200px';
  tooltip.style.backgroundColor = '#eee';

  // Register onclick listener
  tooltip.onclick = function() { alert('hello'); }

  // *** Comment one of these out: ***

  //document.body.appendChild(tooltip);
  document.getElementById('container').appendChild(tooltip);
}

</script>
</head>

<body>

<div id="container" style="border: 1px solid blue; float: left; ">
  <div id="myDiv" 
       onmouseover="makeDiv();" 
       style="position: relative; border: 1px solid red; width: 200px;">

       <span>my div text</span>
  </div>
</div>

</body>
</html>

      

0


source


Here is the code to remove the tooltip for onmouseout.

Give your tool an ID hint when you create it:

toolTip.setAttribute('id','toolTip');

      

Then for onmouseout

function removeDiv(container) {
    var toolTip = document.getElementById('toolTip');
    document.getElementById(container).removeChild(toolTip);
}

      

0


source







All Articles