views:

1100

answers:

5

Hi All,

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 within my page, e.g:

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

and the element is generated and positioned correctly - but the onclick event now is no longer captured. Any thoughts? This is x-browser, so i must be missing something.

Thanks, Don.

A: 

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

grepsedawk
Ah, you beat me to it. I have tested in FF4, IE7, Opera 7 and Chrome,and it works. Can the OP please post a complete example? There must be something else that is wrong.
some
Seconded: please post more code.
annakata
ok, thanks guys, one minute...
We are waiting ;)
some
hi - ok i posted my code below (as an answer), you'll see that it only works with document.body.
A: 

Ok all, here is my code, apologies for the delay. A version with a work-around is posted underneath:

<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 - so this 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>
hi yes, i just updated that.really? not working in FF3.0.4
The problem exists in FF3, IE7 and Chrome, but it works in Opera.
some
Sorry about my comment earlier(I deleted it now). I rushed a little bit. I think I can replicate your problem on my computer. I'll see if I can answer this.
luiscubal
ok i have a work around I think , I will add it to the bottom of my "answer" so you can see (in future I will simple edit my original post ;) ). The problem seems to be caused 'myDiv' being the same element that triggers the creating of 'tooltip' and the element to which tooltip is appended.
+1  A: 

Maybe you need to register the event handler after appending?

Kev
hi - yes doesn't seem to make any difference unfortunately
Ah, okay, thanks for trying it out...
Kev
+1  A: 

You're creating not only one but MANY divs. Try this instead(I hope you don't mind but I fixed 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>
luiscubal
I concur, many divs are created.
some
ah of course! becuase I am triggering mouseover everytime i go to click...
luiscubal - and everyone - thank you very much for the help
Update: yes, because tooltip is a child of 'myDiv' it also triggers the mouseover.
A: 
Casey