Click here to Skip to main content
15,946,179 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
Please check the code.

What I want to do is, i want to add divs dynamically to DivUserMailList(MainDiv) and if I want to delete, I have to delete dynamically.

ie, first I added 5 divs with ids div0 to div4. Now I deleted div2, div5.

Please check str in the code, To remove div, I want to click on the div which was created dynamically by
str<br />

So now I want adding divs,removing divs and remaining div's contents, but the code I wrote is not working in such a way.

for (i = 0; i < tempRes.value.Count; i++) {
           s = "chk" + tempRes.value.UserLoginListDet[i].UserLoginId + i;
           if (document.getElementById(s).checked) {

               str = "<div id='div" + divcnt + "' class='DivUserIds'><div id='DivUid' >" + tempRes.value.UserLoginListDet[i].UserLoginId + "</div><div id='DivUidCross' class=\"CrossIconDiv\" OnClick='RemoveDivElement('DivUserMailList'," + divcnt + ");'></div></div>";
               divcnt = divcnt + 1;


      document.getElementById("DivUserMailList").innerHTML = str;

Please help me.

Thanks in advance.
Updated 16-Apr-11 13:50pm
Indivara 16-Apr-11 19:51pm    
Fixed language (linked from tip

Removing your DIV:

var div = document.getElementById('DOM');

if (div) {    



Add a DIV
var msgContainer = document.createElement('div'); = 'DOM';
msgContainer.className = 'YurClass';
Share this answer
Sridhar Patnayak 16-Apr-11 3:30am    
Please check "str" in the above code, To remove i want to click on the div which was created dynamically by "str", so the code you chosen is not sufficient to do this task
Rather than adding and deleting DIV just make
and style.Display:''
Share this answer
i cant agree more with Koolprasad2003, why dont you just hide them with css. use div id and hide it.
for ex:
<div id="content">
My Content

in style.css


Hope this serves your purpose
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900