You are here : Home - Resources - FAQ - Javascript Q5
FAQ - Javascript Q5
Q5:How do I change the content in a form text box?
A:First make sure your form has a name for example <form name="myform">
Make sure the text box has a name as well e.g <input type=text name="mytext">
How you call the function is up to you e.g. onclick, onchange etc
For this example I have used onChange
Lets look a the code used
<script language="JavaScript" type="text/javascript">
<!--
function changeit(){
ReplaceText = "You typed : "+document.myform.yourtext.value;
document.myform.mytext.value=ReplaceText;
}
// -->
</script>
<form name="myform">
Type your text here<br>
<input type="text" name="yourtext" size="25" onchange="changeit();"><br>
Then click here<br>
<input type="text" name="mytext" size="30">
</form>
The line
document.myform.yourtext.value finds us the value from the document formname fieldname
This
document.myform.mytext.value=ReplaceText; changes the text in document formname fieldname.
Getting values from other form elements such as select boxes etc is a bit more complicated. But that wasn't the question :)
back to faq