Javascript Create A Hyperlink Element

Creating a hyperlink element using Javascript is pretty simple. Do the following. Notice, that the hyperlink element contains two text ‘click me’ that’s been set twice. This is because IE only accepts its proprietary innerText property to set a hyperlink’s text while other browsers use textContent. To set a CSS class name, you need to use the className attribute because the class property is a reserved word in Javascript. You can either use it as element.classname = 'class_name' or element.setAttribute('className', 'class_name');

Related Posts Plugin for WordPress, Blogger...


  1. I have read a few of the articles on your website now, and I really like your style of blogging. I added it to my favorites blog site list and will be checking back soon. Please check out my site as well and let me know what you think.

  2. This is exactly what I have spent a day to find, but can’t seem to make it work. It loads the page without error, but no link appears. How do you apply it in a web page?

    Thanks in advance…

    Here is what I put together from what you posted:

    div = document.getElementById(‘div’);

    a = document.createElement(‘a’);
    a.innerText = ‘click me’;
    a.textContent = ‘click me’;
    a.href = ‘javascript://’;
    a.setAttribute(‘className’, ‘more_right’);
    a.setAttribute(‘onclick’, ‘alert(“clicked”);’);


  3. Sorry – I did copy the whole page, but that’s not what it posted. Could you just use the code in a working example for newbies like me to mimic? Thanx again!

  4. @Dallas: hi. the html code was a shortcut, i figured having the div tags alone would work. you can add tags between the div tags.

    i updated the html code above

Leave a Reply

Your email address will not be published. Required fields are marked *