Forum Moderators: open
<a href="#" onclick="document.forms['name_of_form'].name_of_input.value = 'mynewvalue'; return false;">
The
valueproperty of the <input> element does not contain the single quotes (in this case).
The
name_of_formand
name_of_inputvalues correspond to the values of the
nameattributes on your form and input elements respectively.
Assuming the following HTML:
<form name='yourForm' action="">
<div>
<input type='text' name='yourInput' id='yourInput' value='Hello World'>
<a href='#yourInput' id='yourLink'>Change the world!</a>
</div>
</form>
Define the following script in the head of your document to keep it unobtrusive:
<script type="text/javascript">
// Assign the value to the input with the id passed in
function changeTheWorld( idRef, val ) {
// Make sure browser supports getElementById
if(!document.getElementById ) return;
// Find the input by it's id
var inputObj = document.getElementById( idRef );
if( inputObj ) {
// Update the value
inputObj.value = val;
}
}
// Attach all of the behaviors requiured on the page
function attachBehaviors() {
// Make sure browser supports getElementById
if(!document.getElementById ) return;
// Find the link by it's id
var linkObj = document.getElementById( 'yourLink' );
if( linkObj ) {
// Attach the onclick handler
linkObj.onclick = function() {
changeTheWorld( 'yourInput', 'Hello Dolly' );
};
}
}
// Define the onload method and tell it to attachBehaviors
window.onload = function() {
attachBehaviors();
};
</script>
Just a different approach. :-)