1

I have some jquery/javascript code that alters some things on document ready and when a form input is changed. How do i make this form specific?

$(function() {
    $('input[name="item_price[]"]').live('change', function () {
        var tr = $(this).parent().parent();
        CalculateData(tr);
    });

If my form's id tag is "my_form", how do i make this little snippet occur only if item_price[] in the "my_form" is being changed and not all instances of the item_price[] input?

2
  • Have a look at the descendant selector. The tutorials might be helpful as well. Commented Nov 3, 2011 at 15:07
  • lol fastest gun in the west again Commented Nov 3, 2011 at 15:08

4 Answers 4

1

use the selector code like this $('#my_form input[name="item_price[]"]') instead

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

Comments

1

Simply add the id selector, #my_form, to your existing selector:

$('#my_form input[name="item_price[]"]').live('change', function () {
    var tr = $(this).parent().parent();
    CalculateData(tr);
});

Additional Information:

jQuery selector reference: http://api.jquery.com/category/selectors/

Comments

1

add #my_form to the jquery selector

$('#my_form input[name="item_price[]"]').live('change', function () {
    var tr = $(this).parent().parent();
    CalculateData(tr);
});

Comments

0

You can use delegate in the following way to attach the event handler to the form element. This has the added advantage of jQuery not having to process unrelated events (i.e. ones that originated from outside of the form).

$('form#my_form').delegate('input[name="item_price[]"]', 'click', function() {
    ...
});

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.