Skip to content

Commit

Permalink
Added readme. Moved JS to end of body
Browse files Browse the repository at this point in the history
  • Loading branch information
TBeijen committed Mar 10, 2011
1 parent 895d430 commit 5337539
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
18 changes: 18 additions & 0 deletions README.markdown
@@ -0,0 +1,18 @@
# autofocusBackspace jQuery plugin

jQuery plugin that focuses the first of the matching elements while
preserving the backspace functionality of navigating to the previous page.

Plugin mimics browser backspace behaviour until:
* Actual text has been entered
* The element that received autofocus loses it's focus

## Usage
* Include jQuery
* Include jQuery.autofocusBackspace.js
* Call plugin, example:

$('input').autofocusBackspace();

## TODO:
* If disabled, remove event handlers to prevent unneccessary code execution.
50 changes: 24 additions & 26 deletions public/index.htm
@@ -1,29 +1,27 @@
<!doctype html>
<html>
<head>
<title>jQuery autofocusBackspace plugin</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script src="js/jquery.autofocusBackspace.js"></script>
<script>
jQuery(document).ready(function($) {
$('input').autofocusBackspace();
});
</script>
</head>
<body>
<form>
<p>Pressing backspace will return to the previous page only if:</p>
<ul>
<li>Not having already typed anything in the input field.
<li>Not in the meantime having lost focus.
</ul>
<dl>
<dt><label for="myInput1">This field will get focus:</label></dt>
<dd><input type="text" id="myInput1"></dd>
<dt><label for="myInput2">And this field will not:</label></dt>
<dd><input type="text" id="myInput2"></dd>
</dl>
</form>
</body>
<head>
<title>jQuery autofocusBackspace plugin</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form>
<p>Pressing backspace will return to the previous page only if:</p>
<ul>
<li>Not having already typed anything in the input field.
<li>Not in the meantime having lost focus.
</ul>
<dl>
<dt><label for="myInput1">This field will get focus:</label></dt>
<dd><input type="text" id="myInput1"></dd>
<dt><label for="myInput2">And this field will not:</label></dt>
<dd><input type="text" id="myInput2"></dd>
</dl>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script src="js/jQuery.autofocusBackspace.js"></script>
<script>
$('input').autofocusBackspace();
</script>
</body>
</html>
10 changes: 2 additions & 8 deletions public/js/jquery.autofocusBackspace.js
@@ -1,11 +1,5 @@
/*
* autofocusBackspace
*
* jQuery plugin that focuses the first of the matching elements while
* preserving backspace functionality if no text has yet been entered
*
* TODO:
* - If disabled, remove event handlers to prevent unneccessary code execution
* autofocusBackspace jQuery plugin
*
* Copyright (c) 2011 Tibo Beijen
*
Expand Down Expand Up @@ -35,7 +29,7 @@

// respond to backspace while active
_target.keydown(function(event) {
if (_active && event.keyCode === '8') {
if (_active && event.keyCode === 8) {
history.back();
}
});
Expand Down

0 comments on commit 5337539

Please sign in to comment.