473,387 Members | 1,486 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,387 software developers and data experts.

why doesn't this function (showImage() ) work as soon as the div got content?

Expand|Select|Wrap|Line Numbers
  1. <body>
  2. <div id="myImages"></div>
  3. <! THIS IS THE DIV MY CONTENT NEEDS TO GO IN-->
  4. <div id="viewImage"></div>
  5.  
  6. <script type="text/javascript">
  7. //ajaxrequest
  8. var imageBankXhr = XHR();
  9.  
  10. imageBankXhr.open('get', 'imagebank.php', false);
  11. imageBankXhr.send(null);
  12.  
  13. var myXmlFeed = imageBankXhr.responseXML;
  14. var imageBank = myXmlFeed.documentElement.childNodes;
  15.  
  16. // here i get the images and properties from an external xml
  17. var aFragment = document.createDocumentFragment();
  18. for (var i=0; i<imageBank.length; i++) {
  19.     var newDiv = document.createElement('div');
  20.     newDiv.className = 'myImage';
  21.     var newImage = document.createElement('img');
  22.     newImage.src = imageBank[i].childNodes[0].childNodes[0].nodeValue;
  23.     newImage.imageid = imageBank[i].attributes[0].nodeValue;
  24.     newImage.caption = imageBank[i].childNodes[1].childNodes[0].nodeValue;
  25.     newDiv.appendChild(newImage);
  26.     aFragment.appendChild(newDiv);
  27. }
  28. document.getElementById('myImages').appendChild(aFragment);
  29.  
  30. // ADD EVENT REFERS TO ATTACHEVENT / ADDEVENTLISTENER
  31. var myImageDivs = document.getElementById('myImages').childNodes;
  32. for (var i=0; i<myImageDivs.length; i++) {
  33.     addEvent(myImageDivs[i].getElementsByTagName('img')[0], 'click', showImage);
  34. }
  35.  
  36. /*
  37. THIS FUNCTION WORKS FINE WHEN I ALERT THE RESULT, IT ONLY 
  38. WORKS ONE TIME, WHEN I USE INNERHTML OR TEXTCONTENT OR 
  39. INNERTEXT... AS SOON AS THE DIV HAS CONTENT IT DOESNT WORK 
  40. ANYMORE (only tested it in ff and safari)
  41. */
  42. function showImage(e) {
  43.  
  44.     target = e.target || e.srcElement;
  45.     document.getElementById('viewImage').innerHTML = target.caption;
  46.  
  47. }
  48. </script>
  49. </body>
  50.  
Oct 1 '10 #1
3 2142
kidwon
4
Try with embedding jquery library and put the part of the code that doesn't work soon enough in $("document").ready()
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript" src="path to jquery file"></script>
  2.  
Expand|Select|Wrap|Line Numbers
  1. $("document").ready( function() { .....your problematic code....});
  2.  
It loads the content before the DOM is loaded.
You can right it your own but no need when someone have written it already.
You can find the library at http://www.jquery.com
If any problems feel free to ask again and sign up thus you'll post quickly without moderation!
Oct 1 '10 #2
kidwon
4
Oops now when reading it closely I see that your problem might be a quite different matter...
It's really hard for me to look in other people code but you say that it stops working after content is added to the div. Try removing the content on every iteration but still I'm guessing what's really the problem
Oct 1 '10 #3
Thanks for your reply kidwon. I rewrote the code to this: now it works perfectly, when addNode() is fired, it creates a new div with the content. If there is already a div present in the dom with id: hallo, removeNode() removes it first before a new one is added.

It is the same code, same eventlisteners... So the problem probably exists in the way my clickable images were created (from an external xml file). Or maybe in the for loop that i used to bind them to the click event.

Expand|Select|Wrap|Line Numbers
  1. <body>
  2. <div id='klik'></div>
  3. <div id='klik2'></div>
  4.  
  5. <script type="text/javascript">
  6.  
  7. var newImage = document.createElement('img');
  8. newImage.src = 'images/button.png';
  9. newImage.caption = 'this is an image';
  10.  
  11. var klikme = document.getElementById('klik');
  12. klikme.appendChild(newImage);
  13.  
  14. var newImage = document.createElement('img');
  15. newImage.src = 'images/button.png';
  16. newImage.caption = 'this is an image too';
  17.  
  18. var klikme = document.getElementById('klik2');
  19. klikme.appendChild(newImage);
  20.  
  21.  
  22. addEvent(document.getElementById('klik').childNodes[0], 'click', addNode);
  23. addEvent(document.getElementById('klik2').childNodes[0], 'click', addNode);
  24.  
  25. function addNode(e) {
  26.  
  27. // remove oldNode first
  28.     removeNode();
  29.  
  30.     var newDiv = document.createElement('div');
  31.     var newText = this.caption;
  32.  
  33.     newDiv.id = 'hallo';
  34.     newDiv.innerText = newText;
  35.     newDiv.textContent = newText;
  36.     document.body.appendChild(newDiv);
  37. }
  38.  
  39. function removeNode() {
  40.     if (document.getElementById('hallo') != null) {
  41.         var oldDiv = document.getElementById('hallo');
  42.         oldDiv.parentNode.removeChild(oldDiv);
  43.     }
  44. }
  45.  
  46. </script>
  47.  
Oct 2 '10 #4

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

Similar topics

5
by: TheKeith | last post by:
I'm writing a javascript function that is supposed to open and close a box by adjusting the height style of a div container, everytime the link is clicked. For some reason though, the function only...
6
by: marcelf3 | last post by:
Hello.. This page opens a window with some information, but everytime the user changes a field in the parent window, the child window needs to be closed. These 2 functions were supposed to do the...
0
by: google | last post by:
I am trying to write to a dba and the result is creating a database file, but not writing anything to it. The relevant function is being called, and my variables are populated, but the database...
2
by: Wade | last post by:
I am pretty new to Access, especially writing code, but I found code on the web to do just what I want and it is posted below. It returns a field value from a previous record. I found the code at:...
9
by: xmlhelp | last post by:
stuff.XSL: <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:param name="uid"/> <xsl:template match="/"> parameter...
16
by: deepres | last post by:
Hi, I have the following problem. In my application I'm dynamically replacing some of web-page content. For example, I have something like that: <span id="ui_tr_22_1_treeJ_span" > <div...
4
by: supriyamk | last post by:
Hi, i am trying to copy files of a certain type into a different directory using perl, The copy function doesnt seem to work. my @dir_list; my $sub_dir; my $file_name1; ...
2
by: panos100m | last post by:
Hi on page load a javascript functions (timedCount()) is executed (timer) which executes another javascript function (ajax function:findprov(divtowrite) ) in turn this calls a php file to query a...
13
by: Kong Chun Ho | last post by:
Hi, i have created a XMLHttpRequest function, but it didn't work, this is my code function CheckAccount (acc) { CreateXMLHttpRequest(); if (ready == 1) { if (!xmlHttp) { ...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.