0

I have a form embedded in a web page. When the user clicks submit the XHR response is either a form with all fields reset (i.e. upon success), or a form with error messages (i.e. upon failure). I use the response to overwrite the existing form.

This works the first time the user submits the form. If they submit a second time however, the problem is that the form is posting a full HTTP request.

In the web page the form is wrapped in a span, #add-container. The button within the form is #add-button.

Per the code below, I am trying to re-bind a function to the click event of the buttom whenever the content in the span changes. It seems that this works the first time (i.e. when the document loads), but not subsequent times (i.e. when the XHR response is loaded into the page).

// Add product and services
$(document).ready(function() {
    $("#add-container").change(prepareAddForm());
});

function prepareAddForm() {
    var uri = '/product/add/org/4/format/html';

    $("#add-button").click(function() {
        $("#add-container").load(uri, {language: "php", version: 5});
        return false;
    });
}

Any ideas? Thanks for your assistance...

1
  • Are you sure about invoking the function here: .change(prepareAddForm()); and not passing it as an argument? Commented Oct 12, 2011 at 8:49

1 Answer 1

1

Sorry - just sorted it out. Needed to use the .live() method!

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

1 Comment

Yep, .live() is the correct answer. (or you could just call the prepareAddForm() function again).

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.