Stop <a> element linking action by returning false in its onclick event handler - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Anchor

Description

Stop <a> element linking action by returning false in its 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) {//from   ww w .  j  a v  a2  s.  c  o m
    console.log(elem.href);
    return false;
}

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

Related Tutorials