Javascript Element How to - Get href value from anchor








Question

We would like to know how to get href value from anchor.

Answer


<!DOCTYPE html>
<html>
<body>
  <a id="link" href="http://cnn.com">Page</a>
  <br>
  <script type='text/javascript'>
    var a = document.getElementById('link');
    document.write(a.href + '<br>' + a.getAttribute('href'));
    <!--from  ww w. ja  v a2  s. co  m-->
    </script>
</body>
</html>

The code above is rendered as follows: