Skip to main content

how to set href value of anchor tag in javascript

  • <a id="myAnchor"> Anchor tag </a>
    <script>
       document.getElementById("myAnchor").href = "https://www.cnn.com/";
       let hrefVal = document.getElementById("myAnchor").href ;
       document.write(hrefVal);
    </script>
    Using href property on element you can set the href value in javascript. You can use href property for both set and get the value of anchor tag in javascript.
  • Back to code snippet queries related javascript