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

loading form dynamically doesn't work

P: 21
Hi

I'm trying to load a form into a div (that's hidden and then set to visible) = the form gets a page and stores it inside.
I activate it by pressing a button.

Then the div will fill in some values in the form elements that it has.
however this doesn't work.
the page is loaded but document.getElementById doesn't find it. When I look in
the HTML it's all there.

Expand|Select|Wrap|Line Numbers
  1.     var moth = document.getElementById("sk_p");
  2.     if(moth== null)
  3.     {
  4.         alert(moth);
  5.     }
  6.  
  7.     var intothis = doc.getElementById("sk");
  8.     if(intothis == null)
  9.     {
  10.         alert(intothis)
  11.     }
  12.     //null without ff or no alert in the beginning
  13.  
  14.         for(i=0; i<moth.options.length; i++)
  15.         {
  16.             var newopt = document.createElement("OPTION");
  17.             newopt.value = moth.options[i].value;
  18.             newopt.text =  moth.options[i].text;
  19.             intothis.options.add(newopt);
  20.         }
  21.  
  22.     //providity etc
  23.     var prov = document.getElementById("provide")
  24.  
  25.     if(prov!= null)
  26.     {
  27.         prov.checked = document.getElementById("provide").checked;    
  28.     }
  29.  
  30.  
Apr 4 '07 #1
Share this Question
Share on Google+
5 Replies


ashsa
P: 45
Hi moho,

I don find any error with the script you ve posted.. but check if you have placed the script before or after the body tag in the html. May be its before the body..

If not i think you gotta be bit more clear about your problem.. may be if poss post your html also ..



Hi

I'm trying to load a form into a div (that's hidden and then set to visible) = the form gets a page and stores it inside.
I activate it by pressing a button.

Then the div will fill in some values in the form elements that it has.
however this doesn't work.
the page is loaded but document.getElementById doesn't find it. When I look in
the HTML it's all there.

Expand|Select|Wrap|Line Numbers
  1.     var moth = document.getElementById("sk_p");
  2.     if(moth== null)
  3.     {
  4.         alert(moth);
  5.     }
  6.  
  7.     var intothis = doc.getElementById("sk");
  8.     if(intothis == null)
  9.     {
  10.         alert(intothis)
  11.     }
  12.     //null without ff or no alert in the beginning
  13.  
  14.         for(i=0; i<moth.options.length; i++)
  15.         {
  16.             var newopt = document.createElement("OPTION");
  17.             newopt.value = moth.options[i].value;
  18.             newopt.text =  moth.options[i].text;
  19.             intothis.options.add(newopt);
  20.         }
  21.  
  22.     //providity etc
  23.     var prov = document.getElementById("provide")
  24.  
  25.     if(prov!= null)
  26.     {
  27.         prov.checked = document.getElementById("provide").checked;    
  28.     }
  29.  
  30.  
Apr 4 '07 #2

P: 21
sorry for the code, I use a lot of functions and they are all in separate js files
that are included before the body tag

What I've found out is that:

Expand|Select|Wrap|Line Numbers
  1. alert(intothis.options.length); //var intothis = document.getElementById("sk")
  2. alert(document.getElementById("sk").options.length);
  3.  
gives different answers after code execution:
the first alert gives 8
the second gives 0
so it seems as I'm doing something but not with the real form element.
do I have to replace the original with the one that I've been working with?
Apr 4 '07 #3

P: 21
actually when accessing the page on a old computer I see that the list is populated but then blanked when exiting the loop.
Apr 4 '07 #4

ashsa
P: 45
possible solutions i see are :

1. Try taking the script after the body (if in case you are accessing the element before its appearing on the page, it might not be referred)

2. Since you said you saw the details on a old computer, try using the innerHTML for populating the dropdown instead of using the DOM's createElement("OPTION")

Hope that helps..

actually when accessing the page on a old computer I see that the list is populated but then blanked when exiting the loop.
Apr 4 '07 #5

P: 21
solved the problem, the code was ok, the problem was that the empty form was loaded twice (after populating it)
Apr 17 '07 #6

Post your reply

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