Click to See Complete Forum and Search --> : About "document.createElement(...)"


Caliban
07-21-2003, 09:55 AM
Hi,

I need to create a "< A HREF ...>" element.

I wrote this piece of code to create a hierarchical tree:


(...)
content += "<pre class='level0' style='font-size:10pt' id='parent'>" + TreeArray[0].fullname + "</pre>";
document.write(content);
var newElem, newText, newImg;
for (var i = 1; i <= TreeArray.length-1 ; i++){
newImg = document.createElement("img");
newImg.src = TreeArray[i].children == 0 ? "../imagenes/tree_null_book.gif" : "../images/tree_closed_book.gif";
newImg.style.cursor = TreeArray[i].children == 0 ? "default" : "hand";

//**********************
//CREATE A NEW PRE ELEMENT
//**********************
newElem = document.createElement("pre");
newElem.id = TreeArray[i].id;
newElem.className =
TreeArray[i].className;
newText =
document.createTextNode(" " + TreeArray[i].fullname);
newElem.appendChild(newImg);
newElem.appendChild(newText);

//**********************
//END CREATE A NEW PRE ELEMENT
//**********************

document.getElementById(TreeArray[i].parent).appendChild(newElem);
navigator.appName == "Netscape" ? newImg.setAttribute("onclick", "swapImage(this); toggle(this)") : newImg.onclick = respond;
}
...
etc, etc..
...



I need in some cases to create a element (node) as a hyperlink (in the form of <A HREF></A>), just like I did in the case of the PRE tag.

Can I use a sentence like this:
newHLink = document.createElement("a href")

Any ideas/suggestions?

Thanx in advance.

Khalid Ali
07-21-2003, 10:18 AM
no...
syntax will be
newHLink = document.createElement("a")

href is attribute that you can set using setAttribute("href","http://www.w3c.org");

Caliban
07-21-2003, 10:35 AM
Hi Khalid,
thanx 4 the info

Additionally, how can I set the "label" for the hyperlink?

i.e.

<A HREF='www.w3c.org'> Goto W3C </A>

where "Goto W3C" is the "label" for the hlink.

What's the attribute I need to set, for the text "Goto W3C" to appear in the link?

I think the resultant code would be:

========
newHLink = document.createElement("a");
newHLink = document.setAttribute("href", "www.w3c.org");
newHLink = document.setAttribute("label", "Goto W3C");

========

... don't you?

Again, thanx in advance and for your interesting.