By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,957 Members | 1,213 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,957 IT Pros & Developers. It's quick & easy.

Any easy way to add dynamic elements

P: 13
Hello,

I am new to JS and DHTML. I want to add elements like lable, textbox, button etc on differnt events of the page in a div present in html page.
So, the contents of present div element changes accordingly. At present, I am adding each element in a form & the dynamic form in the div. I am unable to add the form directly, so i have a label in html div & add the form using insertBefore ('dynamicForm', lbl).

Expand|Select|Wrap|Line Numbers
  1.  
  2.    <div id="directDIV" class="directDIV" style="width: 273px; position: absolute; height: 550px; background-color:silver; left: 13px; top: 188px;"> 
  3.         <label id="lbl"></label>
  4.     </div>
  5.  
Now, in another event, again i got to create another form & add in the div. But for that I first got to remove all elements from the div, if any. Then add the new created form. But as i am not able to add form directly using appendchild('newform'). And if I delete all elements than the original alble is also deleted & hence can't use insertBefore().

How do i face and solve this situation. By removing and adding or appending elements in the html div on different events.

Any help is highly appreciated. Please help me to solve this problem.

Thanks
Trupti
May 15 '07 #1
Share this Question
Share on Google+
1 Reply


gits
Expert Mod 5K+
P: 5,387
hi ...

can you tell me what's getting wrong when you try to append your form with:

Expand|Select|Wrap|Line Numbers
  1. // we assume that elementtoappend is a ref to your form
  2. var desiredelement = document.getElementById('directDIV');
  3. desiredelement.appendChild(elementtoappend);
  4.  
if you want to use insertBefore your label, than you mustn't remove your label with id='lbl' ... so during your remove operation you should ask for the id of the element to be removed and if that id equals 'lbl' don't remove it ...

ps: i'm working on the other problem you had with adding elements a generic/reliable way ... without browserdetection ... you remember it? be patient ... i have a lot to do at the office this week ... weekend will come and a solution too ;)
May 16 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.