jQuery this

Video about jQuery this;

Output $(this) to check where it is refering;

<script>
console.log(this);
</script>

<script>
$(function() {
    $('.target').click(function() {
        $(this).toggleClass('btn-primary');
        console.log(this);
        $('.log').html('this: '+this);
    });
 
 
 
    var obj = {
        doIt: function(e) {
            console.log(this);
            $('.log').html('this: '+$(this) );
        }
    }
 
    $('.obj').on('click', $.proxy(obj.doIt, obj))
 
 
});</script>
<div class="well">
    <span class="target btn btn-primary">click me to toggle</span>
    <span class="obj btn">click me obj</span>
    <div class="log">log</div>
</div>

Leave a Reply

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