Jump to content

[SOLVED] Insert a div


The Little Guy

Recommended Posts

I have a div that looks like this:

 

<div id="userComments" style="clear:both;">
     <div id="nextC"></div>
     <div id="nextC1" class="commentBlock">Comment</div>
      <div id="nextC2" class="commentBlock">Comment</div>
      <div id="nextC3" class="commentBlock">Comment</div>
</div>

 

I use ajax to insert a comment, it work, but I need help with something esle.

When I get the text back, it is what the user used for the comment. I want that text to go into "nextC", then rename that id to "next4" (since "nextC3" is the last one), and add a "commentBlock" class to it. After that I want to insert a new "nextC" div. It will be right where the current one is, and the new comment that was just added will be right below it.

 

All in all, I was just wondering, how do I insert a div after another div?

Link to comment
Share on other sites

Something like this?

 

<script language="javascript">
function addcomment(txt){
	var comments = document.getElementsByClassName("commentBlock"); //Get the current number of comments.
	alert(comments.length);
	document.getElementById("nextC").innerHTML = txt; //Add the current text.
	document.getElementById("nextC").className = "commentBlock";
	document.getElementById("nextC").id = "nextC"+(comments.length + 1);

	//Now add the new nextC div
	document.getElementById("userComments").innerHTML = "\n<div id=\"nextC\"></div>\n" + document.getElementById("userComments").innerHTML;
}
</script>

<div id="userComments" style="clear:both;">
   <div id="nextC"></div>
   <div id="nextC1" class="commentBlock">Comment</div>
   <div id="nextC2" class="commentBlock">Comment</div>
   <div id="nextC3" class="commentBlock">Comment</div>
</div>

<br />

<a href="javascript: addcomment('new comment');" />Add Comment</a>

Link to comment
Share on other sites

I got it!

 

This code will insert into the beginning of the "userC" div:

var userC = document.getElementById('userComments');
try{
var myElement = document.createElement('<div id="nextC'+sCom+'" class="commentBlock">'+resp+'</div>');
} catch (e) {
var myElement = document.createElement("div");
myElement.setAttribute("id",'nextC'+sCom);
myElement.setAttribute("class",'commentBlock');
myElement.innerHTML = resp;
}
userC.insertBefore(myElement,userC.firstChild);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.