Page 1 of 1

JavaScript createElement and setAttribute after elementbyid?

Posted: Sat Apr 26, 2008 4:07 am
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>

Re: JavaScript createElement and setAttribute after elementbyid?

Posted: Sat Apr 26, 2008 8:25 am
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);}

Re: JavaScript createElement and setAttribute after elementbyid?

Posted: Sat Apr 26, 2008 12:23 pm
by Kieran Huggins
or in jQuery:

Code: Select all

$('#add_here').append($('<a id="colortransparent"/>'))
:wink:

Re: JavaScript createElement and setAttribute after elementbyid?

Posted: Sat Apr 26, 2008 2:16 pm
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.

Re: JavaScript createElement and setAttribute after elementbyid?

Posted: Sat Apr 26, 2008 2:33 pm
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:

Re: JavaScript createElement and setAttribute after elementbyid?

Posted: Sat Apr 26, 2008 2:47 pm
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.