Get href of anchor <a> element in onclick event handler - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Anchor

Description

Get href of anchor <a> element in onclick event handler

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
   </head> 
   <body> 
      <div> 
         <a href="#link1" onclick="func(this)">Link 1</a> 
         <a href="#link2" onclick="func(this)">Link 2</a> 
         <a href="#link3" onclick="func(this)">Link 3</a> 
      </div> 
      <script type="text/javascript">
function func(elem) {// w w w  . j a  va  2 s  . com
    console.log(elem.href);
    return false;
}

      </script>  
   </body>
</html>

Related Tutorials