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

Help with Hyperlinks

Hi,

This is my first attempt at trying to code using Ajax/Javascript so bear with me if i've missed something obvious.

I'm trying to build a list of links in a div element based on info in an xml file. The problem is in my loop, everytime i create a link they all link to the last record of the xml file, and not the current one.
I know i am looping though the XML ok because all the links have different names. This is the code:

f
Expand|Select|Wrap|Line Numbers
  1. or (var i=0; i < nodes.length; i++)
  2.                 {
  3.                     // Get the lastname element from our XML data document
  4.                     var doc = xmlHttpObj.responseXML;
  5.                     var lastName = nodes[i].nodeValue;
  6.                     var node = doc.selectSingleNode("//Customers/Customer[Lastname='" + lastName + "']");                    
  7.                     var picy = node.selectSingleNode('picture/text()').nodeValue;
  8.  
  9.  
  10.                     var newLink= document.createElement('a');
  11.                       newLink.setAttribute('href','#');
  12.                       newLink.onclick = function () {getImage('placeHolder',picy)};
  13.  
  14.                       var linkText=document.createTextNode(picy);
  15.                       newLink.appendChild(linkText);
  16.                       document.getElementById('LinkList').appendChild(newLink);
  17.                   }
  18.  
Its the newLink.onclick = function () {getImage('placeHolder',picy)}; that is causing the problem, the 'picy' variable is always being passed as the last xml record.

The only thing i can think is happening is it's only writing out the last link, but i cant understand why
Aug 12 '07 #1
1 1089
pbmods
5,821 Expert 4TB
Heya, beakersoft. Welcome to TSDN!

The reason you are getting this behavior is because on this line:
Expand|Select|Wrap|Line Numbers
  1.  newLink.onclick = function () {getImage('placeHolder',picy)};
  2.  
You are creating a function that calls getImage(), with 'placeHolder' as the first parameter, and the variable picy as the second parameter.

Note that as the loop iterates, the value of picy changes, and so all your functions ultimately do the same thing.

Instead, you'll need to create a global object and assign elements based on the id of each link:
Expand|Select|Wrap|Line Numbers
  1. pices[i] = picy;
  2. newLink.id = i;
  3. newLink.onclick = function() { getImage('PlaceHolder', pices[this.id]); }
  4.  
Aug 12 '07 #2

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

Similar topics

1
by: Brandon | last post by:
A newbie question for you all: Suppose you have a webpage with two frames: a main frame, and a second frame. You have several hyperlinks to other websites in the second frame. What you want is...
6
by: Colleyville Alan | last post by:
I have an application that has an Access table that stores the locations of slides in a Powerpoint file. This used to work fine when there were about 4 files and 200 slides. The database would...
0
by: Tim | last post by:
Access 97: I have a table with a hyperlink field that I display on a form. I can click on the form field and the hyperlink activates correctly. However, if I try to activate the hyperlink using VB...
7
by: Chris | last post by:
Hi, I am writing a webpage in C#. Visual Studio .NET. the following code does not provide me with intellisence on the context, session, and page.cache. When I put the large if...then...
5
by: Richard | last post by:
I've created a simple web project, which works great on my local machine. Every page works correctly (hyperlinks, textboxes, validator controls, etc.). I've tried both the X-Copy method and a setup...
4
by: Seefor | last post by:
Hi, I want my text hyperlinks to have a dotted border underneath, so I did this which works fine: a, a:link, a:visited, a:hover, a:active { color: #000; text-decoration: none;
3
by: prinsipe | last post by:
Hi all, what i'm doing is showing/hiding hyperlinks depending on the hyperlink clicked. it is partially working. when i call step1, all hyperlinks are hidden. but then i call step2, single and...
3
by: silversubey | last post by:
Hello All I have an Excel 2003 worksheet that has a column of hyperlinks that point to a web-based program. The hyperlinks are dynamic and work fine. Is it possible to have Excel use Firefox to...
15
by: Prisoner at War | last post by:
Okay, I'd like to create a print-friendly website, so I've been reading up on how to create print-friendly pages (without duplicating my site, of course, by having separate versions of each...
1
by: Rajpreet | last post by:
Hi All, I have a document where in there are many hyperlinks pointing to files located in a paticular directory. My concern is, if these files are moved from this directory to some other...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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
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...

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.