473,396 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Why do these eventlisteners stop listening after firing a funcion once?

Hi, i think i'll try to be a little bit more specific than i was in my last post. I created a few divs with images that i get from a xml file.
After I created them, i binded them to an event using the folowing function.

Expand|Select|Wrap|Line Numbers
  1. function addEvent(target,eventType,eventHandler) {
  2.     if (target.addEventListener) // W3C
  3.         target.addEventListener(eventType,eventHandler,false);
  4.     else if (target.attachEvent) // IE
  5.         target.attachEvent('on'+eventType,eventHandler);
  6. }
  7.  
It seems like the images loose their binding to the eventhandler as soon there is content in the div: viewImage. I can't click another image to change the content of viewImage. When I call the function removeNode from an external button, it will empty the div, and I can load other content again.
I can't call removeNode() at the start of my function showImage() though.

Creating 4 images with inline onclick methods works well too.

Expand|Select|Wrap|Line Numbers
  1. <body>
  2. <div id="myImages"></div>
  3.  
  4. <button onclick="removeNode()">removeNode</button>
  5. <script type="text/javascript">
  6.  
  7. var imageBankXhr = XHR();
  8.  
  9. imageBankXhr.open('get', 'imagebank.php', false);
  10. imageBankXhr.send(null);
  11.  
  12. var myXmlFeed = imageBankXhr.responseXML;
  13. var imageBank = myXmlFeed.documentElement.childNodes;
  14. var aFragment = document.createDocumentFragment();
  15. for (var i=0; i<imageBank.length; i++) {
  16.     var newDiv = document.createElement('div');
  17.     newDiv.className = 'myImage';
  18.     var newImage = document.createElement('img');
  19.     newImage.src = imageBank[i].childNodes[0].childNodes[0].nodeValue;
  20.     newImage.imageid = imageBank[i].attributes[0].nodeValue;
  21.     newImage.caption = imageBank[i].childNodes[1].childNodes[0].nodeValue;
  22.     newDiv.appendChild(newImage);
  23.     aFragment.appendChild(newDiv);
  24. }
  25. document.getElementById('myImages').appendChild(aFragment);
  26.  
  27. // making the images clickable
  28. var myImageDivs = document.getElementById('myImages').childNodes;
  29. for (var i=0; i<myImageDivs.length; i++) {
  30.  
  31.     addEvent(myImageDivs[i].getElementsByTagName('img')[0], 'click', showImage);
  32. }
  33.  
  34. function showImage(e) {
  35.     var target = e.target || e.srcElement;
  36.     var newDiv = document.createElement('div');
  37.     newDiv.id = 'viewImage';
  38.     var newText = target.caption;
  39.     newDiv.innerText = newText;
  40.     newDiv.textContent = newText;
  41.     document.body.appendChild(newDiv);
  42.  
  43. }
  44. function removeNode() {
  45.         if (document.getElementById('viewImage') !== null) {
  46.         var oldDiv = document.getElementById('viewImage');
  47.         oldDiv.parentNode.removeChild(oldDiv);
  48.     }
  49. }
  50. </script>
  51. </body>
  52.  
Oct 1 '10 #1
2 1285
Dormilich
8,658 Expert Mod 8TB
the problem is the div ID. you assign it multiple times (each time, showImage() is called), which is simple wrong (ids must be unique).

IE won’t execute showImage() properly (e is undefined => you have to use the event object e = e || window.event
Oct 1 '10 #2
Hi, thanks for the reply, but this doesn't work either. Using the id=viewImage did work fine when using inline eventbinding inside an <img> tag unfortunately that is not what i am looking for.

Expand|Select|Wrap|Line Numbers
  1. function showImage(e) {
  2.     var target = e.target || e.srcElement;
  3.     var newDiv = document.createElement('div');
  4.     newDiv.className = 'viewImage';
  5.     var newText = target.caption;
  6.     newDiv.innerText = newText;
  7.     newDiv.textContent = newText;
  8.     document.body.appendChild(newDiv);
  9. }
  10.  
Oct 1 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Rory | last post by:
Hello, Little niggling problem I have - just wondered if anyone has any ideas... Is there a way to stop an SQL statement once it has been sent to Oracle? Now I always thought the answer was...
8
by: Tim Bücker | last post by:
Following scenario: The user opens a form, a thread is started to play a sound ... public void playSoundUsingThread() { if (File.Exists(fileLocation)) PlaySound(fileLocation, 0, 0); //...
1
by: Ahmet Gunes | last post by:
Hi all, I check whether session is alive in Page_Load. If session is not alive then I want to navigate to another page, most probably the login page. But when a control, a button for...
11
by: Alan Silver | last post by:
Hello, I have two dropdown controls on a form, representing a month/year combination. The date entered must be in the future, so I wrote a custom validator to check this. Trouble is, the...
7
by: Denise | last post by:
I just realized the DataTable_RowChanging events were firing when I called Fill method of the DataAdapter! It fires TWICE for each row loaded. I thought these were only supposed to be called when...
3
by: Jordan S. | last post by:
I have a client that has an MS Access application that has outgrown MS Access and it's time to migrate to something more substantial. I'm proposing a .NET Windows Forms application. The client...
5
by: darthghandi | last post by:
I've created a class to listen to all interfaces and do a BeginAccept(). Once it gets a connection, it passes the connected socket off and stores it in a List. Next, it continues to listen for...
10
by: ThunderMusic | last post by:
Hi, I'm currently working with sockets. I accept connections using m_mySocket.Listen(BackLogCount); But when I want to stop listening, I shutdown all my clients and call m_mySocket.Close(), but it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.