Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7534975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:10:17+00:00 2026-05-30T06:10:17+00:00

i have a form to submit message. I’d like to submit in an AJAX

  • 0

i have a form to submit message. I’d like to submit in an AJAX way. the following code works, but the newly add ‘reply’ button doesn’t work:

      {% block body %}
<script type="text/javascript" src="{{ url_for('static', filename='jquery.form.js') }}"></script>
<script type="text/javascript">
    // wait for the DOM to be loaded
    $(document).ready(function () {
        $("#message_form").ajaxForm(function () {
            var messageid = '';
            var messagepubdate = 0;
            // GET NEWEST MESSAGE ID
            $.getJSON($SCRIPT_ROOT + '/_get_new_message', function (data) {
                messageid = data.messageid;
                messagepubdate = data.messagepubdate;
                //alert(messageid); //TESTED! the messageid is the newly added message's _id!
                var div = document.createElement("div");
                div.innerHTML = "<li><img src='{{ g.get_user(g.user._id).email|gravatar(size=48)}}'><p> " +
                 "<strong><a href='{{url_for('user_timeline', username=g.user._id)}}'>{{ g.user._id}}</a></strong>&nbsp; " + $('#new_message').val()
                 + "<small>&mdash; " + messagepubdate + "</small><p align='right' style='text-align: right'><small><a href='#' data-messageid='#replies" + messageid + "' class='reply'>Reply</a></small></p><p><br /><div class='replies hidden' id='replies" + messageid + "'></div></p></p></li>";
                $('ul#messages').prepend(div);
            });
            return false;
        });
    });
</script>

for old messages, the following scripts works well to toggle the replies div:

<script type=text/javascript>
    $(function () {
        $('a.reply').click(function () {
            messageid = $(this).attr('data-messageid');
            $(messageid).text('loading');
            $.getJSON($SCRIPT_ROOT + '/_get_replies', { messageid: messageid.substring(8) }, function (data) {
                $(messageid).html(data.result);
                $(messageid).slideToggle(0);
            });
            return false;
        });
    });
</script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T06:10:19+00:00Added an answer on May 30, 2026 at 6:10 am

    The official jQuery 1.7 answer is jQuery.on.

    You can use jQuery.on to subscribe to any event, and you can use jQuery.off to unsubscribe.

    To listen to all of the clicks for an element matching a.reply, regardless of when it was added to the document, you could use the following code:

    $(document.body).on('click', 'a.reply', function() { /* ... */ });
    

    Likewise, you could use

    $('body').on('click', 'a.reply', function() { /* ... */ });
    

    Basically, the $('body') is the context to which the event listener is added. So in reality, jQuery is waiting for any event to bubble up to the body element, and checking to see if that event is a click event. If it is, and the element which gerenated the click event matched the a.reply selector, then jQuery will fire the delegate you passed as the third parameter.

    If we just did this:

    $('a.reply').on('click', function() { /* ... */ });
    

    It would work the same way, but only listen to the events generated by the the elements originally selected by the a.reply selection. Therefore, any new elements added to the dom afterward would not be included. And, because we didn’t add a selector to the on method, it will not listen for child events.

    Edit

    In previous versions of jQuery, you would use jQuery.live, which works in a similar, if not the exact same fashion. In jQuery 1.7, jQuery.live simply wraps jQuery.on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple form to submit and test ajax,but it doesn't work and
I have problem with ajax and php.I have next code $(form).submit(function() { var poruka
I have the following JQuery to submit a contact form with jquery ajax. This
I have a question about Ajax form submit. Here is the code: Controller [HttpPost,
I have this form submit code: Event.observe(window, 'load', init, false); function init() { Event.observe('addressForm',
I would like to have a form that can submit to two different action
I have a form setup but for some reason the JS to submit the
I have trouble getting a flash message to show after a failed form submit.
I have a registration form and am using $.ajax to submit it. This is
I have a script, which let's users submit e message. But I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.