jQuery HTML Element How to - Get anchor href in click event








Question

We would like to know how to get anchor href in click event.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!-- ww w.  ja  v a 2  s. c om-->
    $('#list').on('click', '.status', function(e){
        e.preventDefault();
        console.log(this.href);
    });
});
</script>
</head>
<body>
  <ul id="list">
    <li><a class="status" href="1">link 1</a></li>
    <li><a class="status" href="2">link 2</a></li>
    <li><a class="status" href="3">link 3</a></li>
  </ul>
</body>
</html>

The code above is rendered as follows: