Fabrizio Mazzoni
Fabrizio Mazzoni

Reputation: 1909

jQuery pass function parameter to nested function

I have the following in jquery:

$( ".paid" ).each(function (i, tag) {
    $(tag).change(function (i, tag) {
        if ($(this).is(":checked")) {
            alert($(tag).i);
            $("invdesc_" + i).prop("disabled", false);
        }
    })
});

How can i pass i in the $(".paid") function to the $(tag).change function?

I need to change the .prop() based on the key which is coming from a php foreach() iteration.

Upvotes: 0

Views: 1380

Answers (1)

Amit Joki
Amit Joki

Reputation: 59232

You can do this

$(".paid").each(function(i, tag) {
    $(tag).change({ index: i, elem: tag }, function(e) {
        if ($(this).is(":checked")) {
            alert($(e.data.elem)); // tag
            $("invdesc_" + e.data.index).prop("disabled", false); // i
        }
    })
});

You can pass the eventData any object and retrieve it through event.data

Upvotes: 2

Related Questions