Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

Save Web Form Content Using Control + S

20 Responses »

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!

Discussion

  1. kolin
    October 14, 2009 @ 8:15 am

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

  2. October 14, 2009 @ 8:36 am

    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. October 14, 2009 @ 9:50 am

    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. October 14, 2009 @ 10:39 am

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

  5. October 14, 2009 @ 12:29 pm

    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. October 14, 2009 @ 12:51 pm

    @kolin: Yes, i cameto mention it ;)

    Good work!
    recently i just saw in Hotmail.

  7. October 15, 2009 @ 4:21 pm

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

  8. October 15, 2009 @ 5:24 pm

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

  9. alexander
    October 15, 2009 @ 11:26 pm

    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. October 17, 2009 @ 4:38 am

    hei david can you write for JQuery…?

  11. nikolaj
    October 28, 2009 @ 6:01 pm

    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:
    [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);
    [/code]

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

  12. October 28, 2009 @ 6:14 pm

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

  13. October 28, 2009 @ 8:28 pm

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

    Great job!

    Best regards, MrKenobi

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!