0

I'm working on MVC 3 application, I have managed to create a dynamically created textboxes using partial view with validations for these dynamic boxes, however, I want to check if these dynamic textboxes are changed and change a hidden field value to 'dirty' accordingly, I use the following code to detect any changes on textboxes and it works OK on any already existed textboxes, but it doesn't work on dynamically created textboxes:

<script type="text/javascript">
    $(document).ready(function () {
        $("input[type='text']").change(function () {
            $("#FormState").val('dirty');
        });
    });
</script>

Is there anyway to do this with javascript?

2 Answers 2

3

What you want to do is called event delegation. jQuery currently offers three ways to do event delegation, with live, delegate, and on.

The other answer suggested using live, but it and delegate are deprecated for good reason. They have some performance problems. You should use on for event delegation. In order to use it best you need to give it a reasonable parent element of these inputs. Try to choose the closest parent to all of your inputs.

$('#parent-id').on('change', 'input[type="text"]', function () {
    $("#FormState").val('dirty');
});
0
2

Use:

   $("input[type='text']").live('change',function () {
        $("#FormState").val('dirty');
    });

JQuery Live

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.