JavaScript createElement and setAttribute after elementbyid?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

JavaScript createElement and setAttribute after elementbyid?

Post by JAB Creations »

I'm trying to create an anchor element to be placed after the anchor with the id colortransparent. I'm not sure what I'm doing wrong here...suggestions?

Code: Select all

function add_element() {  new_color = document.createElement('color'+custom_color);  new_color.setAttribute('id', 'stylesheet');  //new_color.setAttribute('tabindex=\"3\"', 'stylesheet');  new_color.innerHTML = 'Im over here!';   my_div = document.getElementById('add_here');  document.getElementById('add_here').insertBefore(new_color, my_div);}

Code: Select all

<div id="add_here" style="clear: left; float: left; margin: 4px;"><a id="colortransparent" href="javascript&#058; anchor();" tabindex="3"></a></div>
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: JavaScript createElement and setAttribute after elementbyid?

Post by EverLearning »

I think this is what you're looking for:

Code: Select all

function add_element() {      var new_color = document.createElement('a');    new_color.setAttribute('href', '#');    new_color.setAttribute('id', 'color'+custom_color);    new_color.innerHTML = 'Im over here!';    referenceNode = document.getElementById('colortransparent');    document.getElementById('add_here').insertBefore(new_color, referenceNode.nextSibling);}
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: JavaScript createElement and setAttribute after elementbyid?

Post by Kieran Huggins »

or in jQuery:

Code: Select all

$('#add_here').append($('<a id="colortransparent"/>'))
:wink:
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: JavaScript createElement and setAttribute after elementbyid?

Post by JAB Creations »

LoL...I know jQuery is awesome but it's an extra 24KB that loads on my site only when visitors choose broadband. That means on a 28K modem many people will have to wait an extra six seconds...and this is with all the jQuery files already compressed. So thanks for the reply but I can't use jQuery to do this.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: JavaScript createElement and setAttribute after elementbyid?

Post by JAB Creations »

Oh I did not spot your post Ever, that worked fine, thanks! I'll have to fine tune my over all script...you guys are going to just love what I'm working on when it goes live. :mrgreen:
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: JavaScript createElement and setAttribute after elementbyid?

Post by JAB Creations »

Err is there a way to add a break line \n before where the new element is inserted? I've tried without quotes, single quotes, and double quotes adding it to the new_color ('\n'+new_color... for example.
Post Reply