jQuery Event How to - Trigger anchor link click from span








Question

We would like to know how to trigger anchor link click from span.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.5.2.js'></script>
</head><!--   w  w w  . ja v a2  s.  c om-->
<body>
  <a id="clickit" href="" onclick="console.log('onclick')">click</a>
  <br/>
  <br/>
  <br/>
  <span onclick="javascript:$('#clickit').trigger('click')">fire the a click!</span>
  
  
  <br/>
  <br/>See console for the output
</body>
</html>

The code above is rendered as follows: