how to set href value of anchor tag in javascript

Answers 1
  • <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>
    
    1
    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