web-profile

jQuery this

Video about jQuery this;

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

console.log(this);

js:

$(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))


});​

html:

<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.