Crosstips.org

My fun Crossword solver project. Crosstips.org & Krysstips.se

Kung Fu

Fujian White Crane Kung Fu

Fry-IT

Fry-IT is the company I work for

Photos

Photoalbum, both old and new.

Zope

What I have and am doing with Zope

Receptsamlingen

In Swedish only. About my "Collection of Recipes" website.

Contact me

My contact details and how to contact me.

 

KungFuPeople.com
Do you train Kung Fu?
Or know someone who does?
Then check out KungFuPeople.com


Mobile version of this page Mobile version of this page


 

createElement('a') with a javascript href


21st of November 2005

In Javascript, have you ever needed to create a hyperlink element like this?:

 div = document.getElementById('foo');
 newlink = document.createElement('a');
 div.appendChild(newlink);

That snippet doesn't do much. It just creates a hyperlink element with no href, class, title or content. Let's make it a bit more useful:

 newlink = document.createElement('a');
 newlink.setAttribute('class', 'signature');
 newlink.setAttribute('href', 'showSignature(xyz)');

The problem with this code is that the href becomes a link to a page called showSignature(xyz) and not a javascript function call to the function showSignature() with parameter xyz.

Solution, which took me some time to figure out, is that you have to use the javascript: prefix on the href for it to become an active javascript function caller. So this is how it should be:

 newlink = document.createElement('a');
 newlink.setAttribute('class', 'signature');
 newlink.setAttribute('href', 'javascript:showSignature(xyz)');

But I've got a sneaky feeling there's something wrong with this. Using the javascript: prefix feels a bit 1999. Can someone fill in my brainblanks on this?

UPDATE Just realised what would have been much better; set the href to # and use the onclick event instead. Problem solved.



Comment

Carlos Blas - 3rd March 2006  [«« Reply to this]
FYI: I am writting a JSP that also dynamically creates an <a href> element and found that the href="#" did not work, but the href="javascript:function" did work. Thanks for the help.
Anonymous - 11th April 2007  [«« Reply to this]
You should never set an href to # for a javascrpit event. You were right to set an onclick event, but the href should really be javascript:void(0) to avoid refresh issues.
Anonymous - 1st June 2007  [«« Reply to this]
how to set onclick event?
RCA - 25th July 2008   [«« Reply to this]
newlink.setAttribute('onclick', 'myHandle(this)');
Anonymous - 25th June 2007  [«« Reply to this]
Yes, how DO you add the onclick event?
Mehrbod - 20th July 2007  [«« Reply to this]
Apparently it works but because we cannot set 'Text' property/attribute of <a> tag we cannot see the generated hyperlinks. That would be great if someone can let me know how to set it.
RobertZDeveloper - 15th November 2007   [«« Reply to this]
try new.innerHTML = 'link_text' to get the text in there
alfred - 14th December 2007  [«« Reply to this]
thank you very much, but i have a question, what about de text you are going to click?
Anonymous - 16th January 2008   [«« Reply to this]
tn = document.createTextNode('link text');
newlink.appendChild(tn);

That will create the clickable text.
Sam P. - 23rd August 2009  [«« Reply to this]
Actually, you should use # for href. The point is that you can avoid using old 'javascript:<...>' style links.

Make sure to 'return false' from your onclick event to prevent submitting your link and any page refresh issues.

Even better, change the # to a POST URL so that it works for people w/o a JS-enabled browser (webphones, etc.)
Anonymous - 25th August 2009  [«« Reply to this]
>>>>> Even better, change the # to a POST URL so that it works for people w/o a JS-enabled browser (webphones, etc.)

if they don't have javascript enabled then the element will not be created!!!!
Email hidden - 2nd October 2009  [«« Reply to this]
I had a problem getting the onclick to work in IE when created with setAttribute. I used this and it worked:

newlink.onclick = function (e) { myHandle(this); }
Don Don - 7th October 2009  [«« Reply to this]
How to make the link with the attribute of target="_new" ? the

newlink.setAttribute('href', 'xyz.html')

would not suffice?

Thanks
43.53 - 24th November 2009  [«« Reply to this]
ok
Anonymous - 12th February 2010  [«« Reply to this]
hello i'm trying to create anchor tag in javascript-

var div1=document.getElementById("tabarea");
var tab=document.createElement("a");
tab.setAttribute("class","tab");
tab.setAttribute("href","a.html");
tab.innerHTML="<a class=\""+"tab"+"href=\""+"a.html"+"\></a>"
div1.appendChild(tab);

here i'm trying to add a tab dynamically(which is my custom tag,created using 'a' tag ) to my tab area.if i add text,button it gets added, but 'a' tag :(
can anyone please help?
 
Name:
Email:
hide my email address.

Your email address will be encoded to prevent email-extraction spiders from reading it so you won't get spammed if you decide to show your email address.