Save Web Form Content Using Control + S

By  on  

We've all used word processing applications like Microsoft Word and if there's one thing they've taught you it's that you need to save every few seconds in anticipation of the inevitable crash. WordPress has mimicked this functionality within their WYSIWYG editor and I use it frequently. Here's how to listen for "CONTROL+S" using MooTools.

The HTML

<form method="post" id="edit-form">
<textarea style="width:400px;height:250px;" name="content" id="content-box"><?php echo $_POST['content']; ?></textarea><br />
<input type="submit" id="save-button" value="Save and Continue" />
</form>

A simple form -- no modifications needed for this functionality to work.

The MooTools JavaScript

(function($) {
	window.addEvent('domready',function() {
		$('content-box').addEvent('keydown',function(event) {
			if((event.control || event.meta) && event.key == 's') {
				event.stop();
				$('edit-form').submit();
			}
		});
	});
})(document.id);

On the keydown event we listen for CONTROL+S (or META+S on Macs) and if the key is "s", we trigger form submission. I've not attempted to show you the AJAX method because that type of system would be very specific to your database/server-side language.

Crazy, sexy, cool, just like TLC from the 90's!

Recent Features

  • By
    Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event...

  • By
    Responsive Images: The Ultimate Guide

    Chances are that any Web designers using our Ghostlab browser testing app, which allows seamless testing across all devices simultaneously, will have worked with responsive design in some shape or form. And as today's websites and devices become ever more varied, a plethora of responsive images...

Incredible Demos

  • By
    Implementing Basic and Fancy Show/Hide in MooTools 1.2

    One of the great parts of MooTools is that the library itself allows for maximum flexibility within its provided classes. You can see evidence of this in the "Class" class' implement method. Using the implement method, you can add your own methods to...

  • By
    Create Keyboard Shortcuts with Mousetrap

    Some of the finest parts of web apps are hidden in the little things.  These "small details" can often add up to big, big gains.  One of those small gains can be found in keyboard shortcuts.  Awesome web apps like Gmail and GitHub use loads of...

Discussion

  1. kolin

    firefox 3.5.3 brings up the save page dialog (as well as saving), but nice idea :)

  2. Confirmed on Windows Firefox. I’ll look for patch. WYSIWYG’s do this so I’m confident there’s a way to prevent it.

  3. Preventing browser’s default behavior is really bad thing to do if you care about usability.

    Anyway, it’s a nice example of enhancing user experience using MooTools—you obviously don’t have to use ⌘+S combination, but it could be anything else.

  4. This would be really awesome if you could override firefox’s default behaviour!

  5. Thank you very much for this post.
    I always wanted to know how to handle keyboard events in mootools and this is an excellent example!

  6. @kolin: Yes, i cameto mention it ;)

    Good work!
    recently i just saw in Hotmail.

  7. Alt+S Perhaps, that’s teh way I see it done usually.

  8. Why not use the brand new Keyboard functionnality from Mootools More?

  9. Alexander

    I’ve got about 50 to 100 textareas on my page and a lot of input fields. Is there a way to attach the key down event to the page and not as suggested to every single input element?

  10. hei david can you write for JQuery…?

  11. Nikolaj

    David, it looks like it’s the alert-box that causes Firefox to fail.

    Either get rid of the alert(), or simply put the alert/saving part into a function with delay(1), and it will work as expected :-)

    Updated code:

    (function($) {
    	window.addEvent('domready',function() {
    		$('content-box').focus();
    		$('content-box').addEvent('keydown',function(event) {
    			if((event.control || event.meta) && event.key == 's') {
    				event.stop();
    				(function () {
    					alert('Saving Content!');
    					//$('save-button').fireEvent('click');
    					$('edit-form').submit();
    				}).delay(1);
    			}
    		});
    	});
    })(document.id);
    

    Important: The small delay IS needed in order to make it work as expected!

  12. @Nikolaj: Great catch! I’ve updated my demo.

  13. Great Idea, David!
    Fantastic Website, i like the Tutorials & Demos.

    Great job!

    Best regards, MrKenobi

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!