views:

50

answers:

3

I have a text box called txtName on my form.

In my page I know I need to place the code in my HEAD tag like so......

<script type='text/javascript' language="javascript">

 document.FormName.txtName.value = "Robert";

 </script> 

But I cant seem to set a value to my textbox txtName with the above code......

A: 

Can you set the id on the textbox? Then you can use getElementByID("textboxid"); Selecting by id is faster.

UPDATE: You need to ensure that the DOM has been load so that your script can locate the element you want to update. One way is:

<body>      
    <form>
    <input id="txtName" name="txtaaaName" type="text" value="notnow"/>
    </form>
    <script type="text/javascript">
        function LoadTextBox(){
            document.getElementById("txtName").value = "ready to go";
        }

        LoadTextBox();
    </script>   
</body> 

An alternative could be where you use onload in the body tag:

<head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <title>Untitled Document</title>
        <script type="text/javascript">
            function LoadTextBox(){
                document.getElementById("txtName").value = "ready to go";
            }
        </script>   
    </head>
    <body onload="LoadTextBox">     
        <form>
        <input id="txtName" name="txtaaaName" type="text" value="notnow"/>
        </form>

    </body> 
David Robbins
The ID is also txtName...........now what must the code look like?
Etienne
Just as I supplied.
David Robbins
+3  A: 

That's because your code get executed before the DOM is created. You can do something like this

window.onload = function() { 
  document.forms['FormName'].elements['txtName'].value = "Robert";
}

or use JQuery

$(function() {
  document.forms['FormName'].elements['txtName'].value = "Robert";
  // for a shorter syntax use id
  $('#txtNameId').val("Robert");
}
Keeper
+1 I was going to post similar answer
J Angwenyi
Where must I place this code?
Etienne
Nothing I do can make this to work.........I place this in everywhere????
Etienne
Inside a <script> tag. If you want to use the JQuery solution you also have to add a <script> to load jquery. Take a look on its site: http://jquery.comDouble check that your for is <form name="FormName"> and your text is <input name="txtName">.Another syntax you can use (but not a "standard" one) isdocument.FormName.txtName.value = "Robert"
Keeper
Inside <script> tag yes I know, but must it all go in the <head> tag? Because it still does not work......
Etienne
Ok I see you also use an ID on your text.Place this in your <head> and it must work:<script type='text/javascript' language="javascript">window.onload = function() { document.getElementById('txtName').value = "Robert";}</script>
Keeper
There must be something wrong with the window.onload because even when i place a alert in like so alert("Hello World"); it does not want to show the alert....
Etienne
http://pastebin.com/XPjTUe6YThis works for me. Try to see if you spot any difference/mistake...
Keeper
MMM, it does work when I use a plain web page..........
Etienne
Then there's something wrong in the page. If you need a hand post a new question and full source of the page.
Keeper
You're using jquery so use this:$(function() { $('#txtName').val('Robert');});but you have to move your <script> below the one which loads jquery.
Keeper
I found the problem!!! There was another damn window.onload!! Thanks for all your help! You rock man!
Etienne
Nice! If you need to have many onload do this:oldonload = window.onload;window.onload = function() { <your code here>; oldonload(); }
Keeper
MMMM, not working........
Etienne
Of course for this to work, it must be the last <script> in your page. But for simplicity if you need many onload events use jquery which handles this problem (and many others) quite well.
Keeper
Thanks, I will stick with the one it works! Thanks Man!
Etienne
+1  A: 

Your script executed before page loaded. Use onload event

Tim