Javascript says "document.getElementById("namedisplay") has no properties

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ratcateme
    New Member
    • Nov 2007
    • 11

    Javascript says "document.getElementById("namedisplay") has no properties

    I have this script
    Code:
    function getname(){
    <script type="text/javascript">
    	var name = prompt('What is your name?');
    	while(name==''){
    		name = prompt('We need your name?');
    	}
    	alert(name);
    	document.getElementById('namedisplay').value=name;
    	
    }
    getname();
    </script>
    <input name="namedisplay" type="text" id="namedisplay" value="" />
    When i run the script it works fine it asks for my name then alerts it back to me but when it is ment to put it into the display box it gets and error saying
    "document.getEl ementById("name display") has no properties"
    i am stumped

    Can you please help

    Thanks Scott
  • mrhoo
    Contributor
    • Jun 2006
    • 428

    #2
    you are assigning a value to an element that has not been introduced yet. Move the script, or assign the value when the document has loaded.

    onload=function (){document.get ElementById('na medisplay').val ue=name};

    Comment

    • Fisher ma
      New Member
      • Nov 2007
      • 9

      #3
      You can put the input element before javascript code.

      [code=javascript]
      <input name="namedispl ay" type="text" id="namedisplay " value="" />

      <script type="text/javascript">
      function getname(){
      var name = prompt('What is your name?');
      while(name=='') {
      name = prompt('We need your name?');
      }
      alert(name);
      document.getEle mentById('named isplay').value= name;
      }
      getname();
      </script>
      [/code]

      Comment

      Working...