Search code examples
hrefgetelementbyid

document.getElementById to include href


<script type="text/javascript">
document.getElementById("IDOFELEMENT");
</script>

What is the correct way to turn this into a link?

Can I write

<script type="text/javascript">
document.getElementById("IDOFELEMENT").href("http://www.address.com");
</script>

Many thanks.


Solution

  • javascript:

    // this changes the href value<br>
    document.getElementById("IDOFELEMENT").href = "http://www.address.com";
    

    and the html:

    <a href="www.toBeChanged.com" id="IDOFELEMENT">To Website< /a>