473,326 Members | 2,124 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,326 software developers and data experts.

Dynamically create an anchor tag

Well,

I've a little trouble that seems unsolvable. It's a stupid question
that born because IE doesn't respect the standard as its usual.

But go to the problem. I create an anchor tag element, set some
properties, but not all works as it should. This is my code.

var anchorTag = document.createElement('a');
anchorTag.appendChild(document.createTextNode(x));

var pathImg = x.replace(' ', '_').replace('è', 'e');
pathImg = "images/" + pathImg + ".jpg";

anchorTag.href = "#";
anchorTag.setAttribute('onclick', 'popImage("' + pathImg + '", "' + x +
'")');
colorSpan.appendChild(anchorTag);
Doing in this way works fine just for Firefox and other browser. IE,
unfortunately, ignore the setAttribute method name.
The only way I've found to let it work on IE and Firefox too is the
line below.

//anchorTag.onclick = function() { "popImage('" + pathImg + "', '" + x
+ "')" }

Unfortunately, since the code above is nested into a for ... next loop
the code above not assign the pathImg value and x value to the content
of the in-line function but just a reference, and this cause an obvious
problem. All elements onclick event call through the popImage function
the same image.
Is there a way to solve this. Where am I doing mistake?

I've tried using the eval() method without results.

Thanks for any help.
Andrew

Dec 12 '06 #1
1 17618

Andrew wrote:
Well,

I've a little trouble that seems unsolvable. It's a stupid question
that born because IE doesn't respect the standard as its usual.
The first part is due to an issue of implementation where there is
nothing to explicitly say how things should work. The second part is
about closures.

[...]
The only way I've found to let it work on IE and Firefox too is the
line below.

//anchorTag.onclick = function() { "popImage('" + pathImg + "', '" + x
+ "')" }

Unfortunately, since the code above is nested into a for ... next loop
the code above not assign the pathImg value and x value to the content
of the in-line function but just a reference, and this cause an obvious
problem. All elements onclick event call through the popImage function
the same image.
Yes, because you have a closure back to pathImg and x, all of the
onclick functions reference back to the same value, which is whatever
they were set to last.
Is there a way to solve this. Where am I doing mistake?
There is a thread here that will help with both questions:

Subject: function syntax question
<URL;
http://groups.google.com.au/group/co...0caf593c683e27
>
--
Rob

Dec 13 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
by: Mark Johnson | last post by:
You have an, a, anchor with href link. Can you use a stylesheet to effectively disable the link, so that you can't click on it; that it will appear simply as text?
2
by: Astra | last post by:
Hi everybody Need your help. I have a DB-extracted list of say 5 items per page, which have links on each one that takes you to more detailed info on the 'clicked' particular item. When you...
27
by: Nicholas Couch | last post by:
I have a little form with a couple of dynamically generated list boxes. When the user makes a selection from the first box, the second box is refreshed. When they make a selection from the second...
5
by: john_williams_800 | last post by:
Hi; I am just starting to use the DOM to do some more advanced javascripting so please be patient with my question if it is an ignorant question. I would like to use the DOM to dynamically...
0
by: Diane Yocom | last post by:
I'm very new to ASP.Net and probably jumped in a little over my head, but... I'm trying to create a user control that will control navigation through my site. It's sortof like Amazon.com, where...
11
by: GaryB | last post by:
Hi Guys, I've been battling with this one for hours - I hope that you can help me! My code modifies the <aon a page, from a standard document link into a link with a tailored onclick event. ...
1
by: PraveenAnekalmat | last post by:
Hi everyone, I'm using anchor within the for loop which means the number of anchors(links) dynamically varies. Within the anchor i'm calling one javascript function and passing the dynamic value...
1
by: saikatbose2005 | last post by:
Hi, Ineed some help from anyone regarding an issue I'm facing. I've created an image dynamically on clicking a button. What I inetend to do is when I click on the created image it will popup a...
4
by: pbd22 | last post by:
Hi. I am trying to add an "onclick" 'event to an anchor tag and this is proving harder than I imagined. The anchor HAS NO ID. And, there will be tons of them on the page. So, I cannot access...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.