jQuery .click()

jQuery .click() bind an event handler to the "click" JavaScript event, or trigger that event on an element;

<script>
$(function(){
    $('.target').click(function() { // bind an event handler to the click event
        $(this).toggleClass('btn-primary');
        $('.log').append(' click-event');
    });
 
    $('.trigger').click(function() { // trigger the click event manually
        $('.target').click();
        $('.log').append(' click-trigger');
    });
});​
</script>
<div class="well">
    <p>
        <span class="target btn btn-primary">click me</span>
        <span class="trigger btn btn-inverse">click me to trigger click event manually</span>
    </p>
    <div class="log alert alert-block">log</div>
</div>

Leave a Reply

Your email address will not be published. Required fields are marked *