Click here to Skip to main content
12,693,891 members (32,563 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET Javascript
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


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 15-Apr-11 22:20pm
Updated 16-Apr-11 14:50pm
Indivara36.6K
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: bad
 
good
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);
  Permalink  
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: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Rather than adding and deleting DIV just make
style.Display:none
and style.Display:''
  Permalink  
Rate this: bad
 
good
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>

Hope this serves your purpose
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 19 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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