loading form dynamically doesn't work

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • moho
    New Member
    • Mar 2007
    • 21

    loading form dynamically doesn't work

    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.getEle mentById doesn't find it. When I look in
    the HTML it's all there.

    Code:
    	var moth = document.getElementById("sk_p");
    	if(moth== null)
    	{
    		alert(moth);
    	}
    	
    	var intothis = doc.getElementById("sk");
    	if(intothis == null)
    	{
    		alert(intothis)
    	}
    	//null without ff or no alert in the beginning
    
    		for(i=0; i<moth.options.length; i++)
    		{
    			var newopt = document.createElement("OPTION");
    			newopt.value = moth.options[i].value;
    			newopt.text =  moth.options[i].text;
    			intothis.options.add(newopt);
    		}
    	
    	//providity etc
    	var prov = document.getElementById("provide")
    	
    	if(prov!= null)
    	{
    		prov.checked = document.getElementById("provide").checked;	
    	}
  • ashsa
    New Member
    • Feb 2007
    • 45

    #2
    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 ..



    Originally posted by moho
    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.getEle mentById doesn't find it. When I look in
    the HTML it's all there.

    Code:
    	var moth = document.getElementById("sk_p");
    	if(moth== null)
    	{
    		alert(moth);
    	}
    	
    	var intothis = doc.getElementById("sk");
    	if(intothis == null)
    	{
    		alert(intothis)
    	}
    	//null without ff or no alert in the beginning
    
    		for(i=0; i<moth.options.length; i++)
    		{
    			var newopt = document.createElement("OPTION");
    			newopt.value = moth.options[i].value;
    			newopt.text =  moth.options[i].text;
    			intothis.options.add(newopt);
    		}
    	
    	//providity etc
    	var prov = document.getElementById("provide")
    	
    	if(prov!= null)
    	{
    		prov.checked = document.getElementById("provide").checked;	
    	}

    Comment

    • moho
      New Member
      • Mar 2007
      • 21

      #3
      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:

      Code:
      alert(intothis.options.length); //var intothis = document.getElementById("sk")
      alert(document.getElementById("sk").options.length);
      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?

      Comment

      • moho
        New Member
        • Mar 2007
        • 21

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

        Comment

        • ashsa
          New Member
          • Feb 2007
          • 45

          #5
          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..

          Originally posted by moho
          actually when accessing the page on a old computer I see that the list is populated but then blanked when exiting the loop.

          Comment

          • moho
            New Member
            • Mar 2007
            • 21

            #6
            solved the problem, the code was ok, the problem was that the empty form was loaded twice (after populating it)

            Comment

            Working...