1

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

3 Answers 3

5

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");
}
Sign up to request clarification or add additional context in comments.

10 Comments

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: jquery.com Double 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) is document.FormName.txtName.value = "Robert"
Inside <script> tag yes I know, but must it all go in the <head> tag? Because it still does not work......
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>
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....
pastebin.com/XPjTUe6Y This works for me. Try to see if you spot any difference/mistake...
|
1

Your script executed before page loaded. Use onload event

Comments

0

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> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.