Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2004
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    create div and append link?

    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.

    Code:
    <html>
    <head>
    <title>DOM</title>
    <script>
    <!--
    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
    	    document.body.appendChild(newDiv)
        }
    // THIS DOES NOT WORK
    /* 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.getElementById("lnk").appendChild(newA);
    document.getElementsByTagName("a")[0].href = "http://www.4cologne.de"
    var newAText = document.createTextNode("Grottenolm in da House");
    document.getElementById("lnk").appendChild(newAText);
    }
    // -->
    </script>
    </head>
    <body background="roadshow_screens_01.jpg" style="background-repeat:no-repeat" onLoad="doDummy()">
    <div id="lnk">Hi@LNK</div>
    </body>
    </html>
    }

  • #2
    New Coder
    Join Date
    Dec 2002
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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";
    newA.appendChild(newAText);
    document.getElementById("lnk").appendChild(newA);


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •