Skip to content

Instantly share code, notes, and snippets.

@dnch
Created October 21, 2011 01:02
Show Gist options
  • Save dnch/1302840 to your computer and use it in GitHub Desktop.
Save dnch/1302840 to your computer and use it in GitHub Desktop.
<form onsubmit="return makeFieldsTheSameValue();">
<!--
It's important that your input fields have unique ID attributes,
otherwise we won't be able to reference them easily via JavaScript.
-->
<label for="field_one">Field One</label>
<input type="text" name="field_one" value="" id="field_one">
<label for="field_two">Field Two</label>
<input type="text" name="field_two" value="" id="field_two">
<input type="submit" name="some_name" value="Submit" id="some_name">
</form>
<script type="text/javascript" charset="utf-8">
// The key function is document.getElementById — using that, we
// can manipulate almost anything we need to.
function makeFieldsTheSameValue() {
// Assuming we nee need the other field to be set the same as
// an existing field, we can get the existing value...
fieldOneValue = document.getElementById('field_one').value
// And then ensure that our other field has the same
document.getElementById('field_two').value = fieldOneValue
// Finally, return true to ensure that the form submits successfully
return true;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment