jQuery HTML Element How to - Trigger a click on anchor link








Question

We would like to know how to trigger a click on anchor link.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.5.2.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!--from  w w  w  . ja v a 2s .  com-->
    $("a").live("click", function(){
        $(this).text("It works!");
    });
    $(document).ready(function(){
        $("a").trigger("click");
    });
});
</script>
</head>
<body>
  <ul id="titleee" class="gallery">
    <li><a href="#inline" rel="prettyPhoto">Talent</a></li>
  </ul>
</body>
</html>

The code above is rendered as follows: