Ok, take a look at this jsfiddle: http://jsfiddle.net/Ralt/mzNtj/4/
Read the comments carefully to understand what this code does:
// First, listen for the "onkeypress" event instead of the "blur" or "change". // Why? For the UX, since the user will think he can click the submit // button as soon as the third field is filled in if you use the "blur" or // "change" event. document.forms[ 'achievementf1' ].onkeypress = function( e ) { // Some cross-browser handling var e = e || window.event, target = e.target || e.srcElement; // If the target is an INPUT if ( target.nodeName === 'INPUT' ) { // Now here is the tricky part: // The "every" method returns false if *one* callback returns false, // otherwise it returns true. // And we use !! to check if it not empty. var allNotEmpty = [].every.call( this.elements, function( el ) { return !!el.value; } ) // If it true, it means all the fields are not empty if ( allNotEmpty ) { // So let create an input var newInput = document.createElement( 'input' ) // Set some properties // And then... insert it before the submit button :-) this.insertBefore( newInput, this.elements[ 'submit1' ] ); } } }
I know my code is weird, because I really like the cross-browser for event processing, but every
not supported by outdated browsers, Oh, good ...
source share