Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

populate textbox onload 1

Status
Not open for further replies.

jagprg

Programmer
Jun 16, 2005
11
US
Hi,

I have text box in a form which I want populate when the form loads. I am using <html:text> tags for text boxes, I tried the following but dosent seem to work. any ideas thanks in advance.

Code:
  <script language="JavaScript">
    setCode()
    {
        if(this.form.inputSIC.value == "")
        {
            alert("inside if");
            this.form.inputSIC.value = 100;
            this.form.inputName.setfocus();
        }
    }

</script>



<body onload="setCode()">......
<html:text property="inputSIC" size='10'...
 

Drop the word function before your function definition at the very top:
Code:
function setCode()

Cheers,
Jeff

 
Hi Jeff,

thanks for the reply, I get this error

this.form.inputSIC is null or not an object.

is there anything that I should do?
 

Well... most likely it's having trouble with your use of the form. Try changing your code to use the following:
Code:
var myInput = document.forms[0].elements['inputSIC'];
if(myInput.value == "") {
  myInput.value = 100;
  myInput.setfocus();
}

This assumes that there is a form on the page, and that it is the first form (if there are multiple) and that it contains an element named 'inputSIC'. You would normally add some error checking code before you moved this to production :)

If it gives the same error, use 'view source' in the browser and check what actual HTML (and Javascript I guess) you are seeing on the page. It may be that the form tag is not closed, is missing etc.

Cheers,
Jeff

 
Thanks Jeff,

It did work, now what would I need to pass so as to include the script in an external file .js file

thanks
 

Cut all the lines contained within the <script>...</script> tags and paste them into a fresh file (save it as yourfilename.js -- just a plain text file with a .js extension).

Get rid of the (now defunct) <script>...</script> tags and replace them with this one:
Code:
<script type="text/javascript" src="yourfilename.js"></script>
If you move the .js file to another directory, remember to reference it relative to the HTML file.

Hope that makes sense [smile]

Jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top