Click here to Skip to main content
14,599,201 members
Rate this:
Please Sign up or sign in to vote.
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.
Posted
Updated 16-Apr-11 13:50pm
v4
Comments
Indivara 16-Apr-11 19:51pm
   
Fixed language (linked from tip http://www.codeproject.com/Tips/182522/StringBuilder-in-Javascript-asp-net-3-5-and-above-.aspx)
Rate this:
Please Sign up or sign in to vote.

Solution 1

Removing your DIV:

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

if (div) {    

div.parentNode.removeChild(div);

}


Add a DIV
var msgContainer = document.createElement('div');
msgContainer.id = 'DOM';
msgContainer.className = 'YurClass';
msgContainer.appendChild(document.createTextNode(msg));
document.body.appendChild(msgContainer);
   
Comments
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
Rate this:
Please Sign up or sign in to vote.

Solution 2

Rather than adding and deleting DIV just make
style.Display:none
and style.Display:''
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

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
</div>


in style.css

#content
{
  display:none;
}

Hope this serves your purpose
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100