Javascripted Useable Forms
May 28th, 2007 LukeWith the rising popularity of Ajax, more and more people are starting to build webapps that rely heavily on Javascript. In a lot of cases, the HTML
However, a problem with this style is that, at least in some cases, the new javascript controls mean that the form no longer works as the user expects. This is bad for usability, and I’ve managed to figure out that the whole problem can be resolved (and unobtrusively, no less) by making use of the
If a user submits a form and there is a value for its
When we write
By combining the
<script>
var d = document; // shorthand variable
function handleFormSubmission() {
var inputValue = d.getElementsByTagName('input')[0].value;
alert('The Value of the input is: ' + inputValue);
}
</script>
<form onsubmit='handleFormSubmission(); return false;' action='http://girasquid.com/sample_code/jsdisabled.shtml'>
<input type='text' />
<input type='submit' />
</form>
If you want to see how it turns out in an actual page, take a look at the Hardcoded onsubmit sample code I’ve put up.
This method involves painstakingly hard-coding the
The answer is yes. Here’s the new code:
<script>
var d = document; // shorthand variable
function handleFormSubmission() {
var inputValue = d.getElementsByTagName('input')[0].value;
alert('The Value of the input is: ' + inputValue);
}
// This anonymous function will attach our 'onsubmit' action for us, so that we don't add anything to our HTML markup.
window.onload = function() {
d.getElementsByTagName('form')[0].setAttribute('onsubmit','handleFormSubmission();return false;');
}
</script>
<form action='http://girasquid.com/sample_code/jsdisabled.shtml'>
<input type='text' />
<input type='submit' />
</form>
To see it in action, check out my window.onload onsubmit sample code.