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 8387455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:12:57+00:00 2026-06-09T18:12:57+00:00

I am working on a webpage which has a comment box using which a

  • 0

I am working on a webpage which has a comment box using which a person can comment, these comments are displayed on the same page dynamically using AJAX. For example, when you submit a comment it gets posted on top of the previous comments.

Here is what i’m doing in AJAX:

xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

            var newDivElement = document.createElement('form');
            newDivElement.setAttribute('id',"postForm");
            newDivElement.setAttribute('action',"updateRecords.php");
            newDivElement.setAttribute('method',"post");
            newDivElement.innerHTML=postContent; 

            document.getElementById('mainPostContainer').appendChild(newDivElement);
        }
    }

(postcontent consists of a div which contains the comment)

Now, if I post two consecutive comments, say “this is 1st comment” and “this is 2nd comment”, they get posted in the following order:

<div id=mainPostContainer>
    <form id="postForm" action="updateRecords.php" method="post"><div id="post"> this is 1st comment </div></form>
    <form id="postForm" action="updateRecords.php" method="post"><div id="post"> this is 2nd comment </div></form>
</div>

But I want them in the following order:

<div id=mainPostContainer>
    <form id="postForm" action="updateRecords.php" method="post"><div id="post"> this is 2nd comment </div></form>
    <form id="postForm" action="updateRecords.php" method="post"><div id="post"> this is 1st comment </div></form>
</div>

I tried using insertBefore() instead of append, but I dont know how to get the previous element (ie. the last created <form>).

Am I doing something wrong here? Or is there a better way to do what I am trying to do? Maybe using jQuery?

EDIT: I am using <form> because there are input elements of type button and text inside the form, I didnt mention them just to keep it simple.

  • 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-09T18:12:58+00:00Added an answer on June 9, 2026 at 6:12 pm

    Adding New Element as the First Child

    Plain Old JavaScript

    HTML:

    <div id="mainPostContainer"><p>1</p></div>
    

    JavaScript:

    var myContainer = document.getElementById("mainPostContainer");
    var p = document.createElement("p");
    p.innerHTML = "2";
    myContainer.insertBefore(p,myContainer.firstChild);
    

    Example:

    jsfiddle


    jQuery

    HTML:

    <div id="mainPostContainer"><p>1</p></div>
    

    JavaScript:

    var p = $("<p>2</p>");
    $("#mainPostContainer").prepend(p);
    

    Example:

    jsfiddle

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

Sidebar

Related Questions

I'm working on application which has workflow like this: 1.parsing home page (using HttpURLConnection,
I working on a web page which has a similar structure like this: <div
I'm designing a webpage which has a menu bar with links on the page.
I've been working through the tutorials on this webpage which progressively creates a bootloader
hello all i am working on a project in which i have a webpage
I'm working on a quite simple Webpage (MVC2), using localisation based on Resource Files.
I'm currently working in an application that has to navigate a webpage and recollect
Currently i'm working on my final year project. In a webpage which loads data
I'm working on a project which is using Selenium and I'd like to try
I've got a webpage built using MVC3 and .NET 4 Validation is working great

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.