0

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.

2
  • change the arguments $(tag).change(function (j, tag1) Commented Oct 16, 2014 at 15:10
  • This seems overly complicated. Can you provide same HTML as there will certainly be a much simpler way of doing this. Commented Oct 16, 2014 at 15:29

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.