View Full Version : create div and append link?

Alex 4 Cologne
08-26-2004, 07:04 PM
Hi. I created divs by DOM used as hitboxes. Now I try to add a link to it, so I can click on a div and will be redirected. You can copy the following code into an empty html file and test. If you got a solution for me I'd be happy. Further explanation commented right in code.

divs = new Array()
divs[1] = new Array('110px','655px','133px','83px','roadshow_screens_02.jpg')
divs[2] = new Array('205px','510px','133px','83px','roadshow_screens_03.jpg')
divs[3] = new Array('205px','655px','133px','83px','roadshow_screens_04.jpg')
divs[4] = new Array('300px','510px','133px','83px','roadshow_screens_05.jpg')
divs[5] = new Array('300px','655px','133px','83px','roadshow_screens_06.jpg')
function doDummy(){
for(i = 1; i < divs.length; i++){
var newDiv = document.createElement("div")
newDiv.style.position = "absolute"
newDiv.style.top = divs[i][0]
newDiv.style.left = divs[i][1]
newDiv.style.width = divs[i][2]
newDiv.style.height = divs[i][3]
newDiv.style.href = divs[i][4]
newDiv.style.border = "1px solid blue"
newDiv.id="hitbox" + i
newDiv.style.zIndex = i
/* In the end this should add a link to every div created above.
/* At the moment I just try to add a working link to the div in the body (static)
/* I got confused, because I don't know how to create first an "a" element,
/* then a "href" property and a text to click on.
/* Finally it would be very nice if the a element would be around the div
/* "<a href="http://gonuts.com"><div>I am teh mofo hitbox!11</div></a>"
/* thx!
var newA = document.createElement("a");
document.getElementsByTagName("a")[0].href = "http://www.4cologne.de"
var newAText = document.createTextNode("Grottenolm in da House");
// -->
<body background="roadshow_screens_01.jpg" style="background-repeat:no-repeat" onLoad="doDummy()">
<div id="lnk">Hi@LNK</div>

08-31-2004, 01:00 AM
You should be appending the textNode to the A element, like this....

var newA = document.createElement("a");
var newAText = document.createTextNode("Grottenolm in da House");

newA.href = "http://www.4cologne.de";