1

I am trying to trigger a post request when a user clicks the submit button for a comment. Right now clicking the submit button triggers nothing, I even tried to console.log($(this)) I got no output. The first code below is the jQuery event code and ajax code. The block of code below that is the html button. thanks for the help.

 $('#submitComment').on('click', '#content', function() {
        $.ajax({
            url: 'http://localhost:3000/comments',
            type: 'POST',
            data: data = { comment: {
                body: $(this).find('input[name="createComment"]').val(),
                user_id: 1,
                image_set_id: 2}
            }
        }).done(function(response) {
            console.log(response);
        });
    });

the button I am trying to target with jQuery event

<div class="container">
    <div id="content">
      //the code between comments is in a Handelbar template
        <input name="createComment">
        <button type="submit" id="submitComment">Create Comment</button>
       //end of handelbar template
    </div> 
</div>
1
  • 2
    Swap #submitComment and #content in your .on(). Commented Nov 12, 2014 at 2:36

1 Answer 1

1

You need to bind the delegated handler to an ancestor(#content) of the target element(submitComment)

$('#content').on('click', '#submitComment', function () {
    $.ajax({
        url: 'http://localhost:3000/comments',
        type: 'POST',
        data: data = {
            comment: {
                body: $(this).find('input[name="createComment"]').val(),
                user_id: 1,
                image_set_id: 2
            }
        }
    }).done(function (response) {
        console.log(response);
    });
});
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.