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 7 of 7
  1. #1
    Regular Coder Jesper Møller's Avatar
    Join Date
    Jun 2006
    Location
    Denmark, Copenhagen
    Posts
    138
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Question Make <div> to <a href="">

    How do i make this <div id="her"> URL</div>

    to a link like this <a href="http://thesite/thefolder/thedocument.html?detect=false">Link</a>


    So far i have this

    function writeLink() {
    var locationElement = document.getElementById("her");
    var linkText = "Link";
    var urlNode = document.createTextNode("<a href=\""+window.location.href + "?detect=false\">"+linkText+"<\/a>");
    locationElement.appendChild(urlNode);
    }

    but that dosnt make it a link
    "True knowledge exists in knowing that you know nothing."

    "Education is learning what you didn't even know you didn't know!"

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,511
    Thanks
    3
    Thanked 504 Times in 491 Posts
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    How do i make this
    
    <div id="her"> URL</div>
    
    
    <input type="button" name="" value="DoIt" onclick="writeLink();"/>
    to a link like this <a href="http://thesite/thefolder/thedocument.html?detect=false">Link</a>
    
    
    So far i have this
    <script language="JavaScript" type="text/javascript">
    
    
    function writeLink() {
     var locationElement = document.getElementById("her");
     var lnk=document.createElement('A');
     lnk.href=window.location.href + '?detect=false\ ';
     lnk.appendChild(document.createTextNode('Link'));
     locationElement.parentNode.replaceChild(lnk,locationElement);
    }
    
    
    
    </script>
    
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    Regular Coder Jesper Møller's Avatar
    Join Date
    Jun 2006
    Location
    Denmark, Copenhagen
    Posts
    138
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thanks
    "True knowledge exists in knowing that you know nothing."

    "Education is learning what you didn't even know you didn't know!"

  • #4
    Senior Coder coothead's Avatar
    Join Date
    Jan 2004
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,700
    Thanks
    0
    Thanked 233 Times in 228 Posts
    Hi there Jesper Møller,

    as I have just finished this example, I may as well post it - even though it's a little bit late.
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    <script type="text/javascript">
    <!--
    window.onload=function() {
       obj=document.getElementById('her');
       obj.onclick=function() {
       obj.firstChild.data='';
       link=document.createElement('a');
       link.setAttribute('href','http://thesite/thefolder/thedocument.html?detect=false');
       linkText=document.createTextNode('Link');
       link.appendChild(linkText);
       obj.appendChild(link);
      }
     }
    //-->
    </script>
    
    </head>
    <body>
    
    <div id="her">URL - click for link</div>
    
    </body>
    </html>
    
    
    
    
    coothead

  • #5
    Regular Coder Jesper Møller's Avatar
    Join Date
    Jun 2006
    Location
    Denmark, Copenhagen
    Posts
    138
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thank you coothead :-)

    I think i understand you version a little better ;-)
    "True knowledge exists in knowing that you know nothing."

    "Education is learning what you didn't even know you didn't know!"

  • #6
    Senior Coder coothead's Avatar
    Join Date
    Jan 2004
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,700
    Thanks
    0
    Thanked 233 Times in 228 Posts
    Hi there Jesper Møller
    I think I understand your version a little better
    It was a good job that I posted it then.
    When I saw that Vic had posted a solution, I nearly popped my little effort into the Recycle Bin.

    coothead

  • #7
    Regular Coder Jesper Møller's Avatar
    Join Date
    Jun 2006
    Location
    Denmark, Copenhagen
    Posts
    138
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Glad you dint ... a newbee like me like it when it makes sens .. not youst works
    "True knowledge exists in knowing that you know nothing."

    "Education is learning what you didn't even know you didn't know!"


  •  

    Posting Permissions

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