osmanraifgunes
osmanraifgunes

Reputation: 1468

jquery .live( ) passing argument

I want to pass the width and height parameter to the .live() function. It works but it does not see (this) tag. Shortly, is it possible to pass value to the live function in jquery?

Upvotes: 1

Views: 2170

Answers (2)

Ricardo Lohmann
Ricardo Lohmann

Reputation: 26320

jQuery(document).on('.yourElement', 'event', function() {
    var height = 800;
    var width = 400;
    jQuery(this).height(height);
    jQuery(this).width(width);
});

or if you want to you live:

jQuery('.yourElement').live('click', function(){
    var height = 800;
    var width = 400;
    jQuery(this).height(height);
    jQuery(this).width(width);
});

If you don't want to use on I recommend .delegate():

jQuery(document).delegate('.yourElement', 'click', function(){
    var height = 800;
    var width = 400;
    jQuery(this).height(height);
    jQuery(this).width(width);
});

Upvotes: 1

jorgehmv
jorgehmv

Reputation: 3713

it is not posible, but inside the function you have access to this instance which will be the html element that triggered the event:

    $("#element").live("click", function(){
        var width = $(this).width();
        var height = $(this).height();
    })

it would be the same using on() since as @FAddel says it is now the recommended way

    $("#element").on("click", function(){
        var width = $(this).width();
        var height = $(this).height();
    })

Upvotes: 1

Related Questions