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

  • SEARCH
  • Home
  • 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 7886401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:18:32+00:00 2026-06-03T05:18:32+00:00

I have a question regarding jQuery’s live() function. I am programming a forum. As

  • 0

I have a question regarding jQuery’s live() function.

I am programming a forum. As soon as someone posts something and hits enter, jQuery appends the other posts and also inserts a textarea for comments, to which the following event handler applies:

//Comment on a post
$('.commenttext').keyup(function(e) {
    if (((e.keyCode || e.which) == 13) && !event.shiftKey) {
        comment($(this));
    }
});

The function to post a comment is then called – well at least it should be. For the old posts it works fine, but not for the one that was just posted and appended.

I know that it is possible to preserve functionality using the live() function. However, as you can see, the post gets submitted when hitting enter, there is no button involved. So I wonder how to combine these things, i.e. using live() but without click: ?

FYI, the function to post something looks like this:

//Function to post
function post()
{
    //Get posttext and preserve line breaks
    var posttext=$('#posttext').val();

    //Ajax if posttext is not empty
    if(posttext!="")
    {
        $.ajax({
            //blablabla
            success: function(postid){

                //Prepend posts with the new post
                var newpost=posttext+'<br/><textarea id="commenttext'+postid+'" class="commenttext" placeholder=" Comment..."></textarea>';
                $(newpost).hide().prependTo('#postsDiv').fadeIn('slow');

            }
        });
    }
}

UPDATE 1:

I have changed the event handler to post something to this, which posts fine, but still the functionality is not there:

//Post something
$('#postDiv').on('keyup', '#posttext', function(e) {
    if ((e.which == 13) && !event.shiftKey) {
        post($(this));
    }
});

UPDATE 2:

It works for now 🙂 I didn’t know both comment() and post() have to be live.
I now have the following two functions:

//Post something
$('#postDiv').on('keyup', '#posttext', function(e) {
    if ((e.which == 13) && !event.shiftKey) {
        post($(this));
    }
});

and

//Comment on a post
$('.commenttext').live('keyup', function(e) {
    if (e.which == 13 && !event.shiftKey) {
        comment($(this));
    }
});

It works fine, but it would be better to also use on() to comment. I have tried this:

$('.commentsDiv').on('keyup', '.commenttext', function(e) {
    if ((e.which == 13) && !event.shiftKey) {
        post($(this));
    }
});

but it doesn’t work – how come? commentsDiv is the parent element of commenttext, which is the comment textarea. Do I need to address it with an id?

Thanks 🙂

  • 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-06-03T05:18:34+00:00Added an answer on June 3, 2026 at 5:18 am

    .live can be used with any event you want, not just click (even custom events).

    $('.commenttext').live('keyup', function(e) {
        if (e.which == 13 && !event.shiftKey) {
            comment($(this));
        }
    });
    

    NOTE: If you are using jQuery 1.7+, you should no longer use .live, you should use .on instead.

    $(document).on('keyup', '.commenttext', function(e) {
        if (e.which == 13) && !event.shiftKey) {
            comment($(this));
        }
    });
    

    Instead of document, you should use the closest parent (this element needs to not be removed from the DOM though, if it’s removed, the events are removed too).

    P.S. e.which is normalized in jQuery, meaning it does e.keyCode || e.which for you.

    Docs: http://api.jquery.com/category/events/event-object/

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

Sidebar

Related Questions

I have a simple question regarding jQuery selectors. I'm using the tablesorter and I
Again I have a question regarding this plugin: http://t.wits.sg/2008/06/20/jquery-progress-bar-11/ What I want to achieve
I have question regarding the use of function parameters. In the past I have
I have a question regarding inheritance with function objects. I guess this must have
I have a question regarding jQuery. I have multiple images that are, in essence,
I have a Question regarding Jquery ui dialog boxes. What i have/i did: I
i have a question regarding Jquery <select id=thechoices> <option value=0>Select Box</option> <option value=box1>Box 1</option>
I have a question regarding JSON. I am using a jquery plugin which expected
I have a question regarding why the following returns inconsistent values (function(ab, $, undefined
I have a basic question regarding jQuery. I have a static time-selection box IE:

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.