By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,287 Members | 2,443 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,287 IT Pros & Developers. It's quick & easy.

appendChild objects not recognized?

P: 13
I'm using the xmlHttpRequest object to gather images and place them on a page. Later in the script, I need to add a function to their onClick property so they can do the business I need them for. However, after having placed them on the page, I cannot then snatch them up again to add the onClick.

I'll readily accept the premise that I'm doing something backwards, but because a) the function I want to add to the onClick property is a method of the kioskDemo prototype and b) I want to pass the 'this' object to the method to make it work, it seems to me that I would want to add the onClick method later in the program, rather than when I'm building it in the getImages method.

Here's the relevant code:


Expand|Select|Wrap|Line Numbers
  1. kioskDemo.prototype.getImages = function() {
  2.     var source = '/global/mul/digital/kiosk/xml/'+this.page+'_'+this.country+'.xml';
  3.     HTTP.getXML(source, return_result);
  4.     function return_result(result) {
  5.         var items = result.getElementsByTagName('collection')[0];
  6.         for(x = 0; x<items.childNodes.length; x++) {
  7.             if(items.childNodes[x].tagName) {
  8.                 var images = items.childNodes[x];
  9.                 var nameTag = images.tagName.toString();
  10.                 var target = document.getElementById(nameTag);
  11.                 for(y = 0; y < images.childNodes.length; y++) {
  12.                     if(images.childNodes[y].tagName) { 
  13.                         var hlink = document.createElement('A');
  14.                         hlink.setAttribute('href','javascript: void(0);');
  15.                         hlink.setAttribute('class', 'k_button');
  16.                         // addEvent(hlink, 'click', kioskChangeFile);
  17.                         var image = document.createElement('img');
  18.                         image.src = images.childNodes[y].getAttribute('src');
  19.                         image.id = images.childNodes[y].getAttribute('name');
  20.                         target.appendChild(hlink); 
  21.                         hlink.appendChild(image);
  22.                     }
  23.                 }
  24.             }
  25.         }
  26.     }
  27. }
  28.  
  29.  
  30.  
  31. function launchKiosk() {
  32.     k.getImages();
  33.     var demo_container = document.getElementById('demo_container');
  34. //  A bunch of other unrelated code. . .  
  35.     var img_buttnz = document.getElementsByTagName('A');
  36.     alert(img_buttnz.length);
  37.     /* at this point, I found out that I cannot see these objects at all. */
  38. }
Oct 20 '08 #1
Share this Question
Share on Google+
2 Replies


iam_clint
Expert 100+
P: 1,208
Expand|Select|Wrap|Line Numbers
  1. target.appendChild(hlink);  
  2. hlink.appendChild(image); 
  3.  
You should really append image to hlink before you append hlink to target
Expand|Select|Wrap|Line Numbers
  1. hlink.appendChild(image); 
  2. target.appendChild(hlink);  
  3.  
Also give your hlink a name and id in the loop.
Oct 20 '08 #2

P: 13
Also give your hlink a name and id in the loop.
iam_clint, thanks very much for the quick reply, that helped!
Oct 20 '08 #3

Post your reply

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