PDA

View Full Version : Forms and Inputs


Dalziel
02-12-2003, 01:12 PM
Is there anyway to set the value of an input text box with Javascript when the form and input have no name?


<html>
<head>
<title></title>
</head>
<body>
<form>
<input type="text" size="20">
</form>
</body>
</html>


Would there be any way to put a script in there to make the input box say something onload? I think its something like document.forms[0].something.value????

head8k
02-12-2003, 01:18 PM
<body onload="document.forms[0].elements[0].value='hello';">

would set the value of the first element in the first form on the page to "hello".

Is that what you mean?

Dalziel
02-12-2003, 03:19 PM
Thanks, can I make a function change the value of the box after the page has loaded?

head8k
02-12-2003, 04:58 PM
Yes, very easily. Put something like this in the <head>

<script language="javascript">
<!--
function whatever(){
//code here to change the value
document.forms[0].elements[0].value='new value here'
}
//-->
</script>

and call it like this:

<body onload="whatever()">

head8k
02-12-2003, 05:00 PM
Re-reading the post, maybe you mean later still?

how about changing it with a link...

<a href="javascript:void(0)" onclick="whatever()">change value</a>

use the same function as above.