Skip to content

amiel/typingIntent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 

Repository files navigation

Typing Intent jQuery plugin

The typing intent plugin calls a callback after a user stops typing.

It does this by handling keyup and setting a timeout that gets reset on each keyup.

Examples

$("#q").typingIntent(function(e) {
  console.log("User finished typing (for the moment) in", this);
  console.log("The original keyup event:", e);
});

Ajax examples

Basic search

(function() {
  var ajax_request;

  $("#q").typingIntent(function() {
    ajax_request = $.get("/search.json", { q: this.val() }, function() {
      // handle search results
    });
  }, function() {
    // this second function is called immediately on keyup
    // we use it to cancel the ajax request if it is still running
    // immediately, when the user starts typing again

    if (ajax_request) ajax_request.abort();

    // this would also be the place to handle key up and down events to navigate through the search results
  });  

})();

Background form verification

(function() {
  var ajax_request;

  $("#user_zipcode").typingIntent(function() {
    ajax_request = $.get("/users/check_zipcode.json", { zipcode: this.val() }, function(zipcode_data) {
      // show zipcode information
    });
  }, function() {
    if (ajax_request) ajax_request.abort();

    if (! (/^\d{5}(-\d{4})?/).test(this.val())) {
      // returning false will prevent the first function from being run
      // we don't need to send the ajax request unless there is a valid zipcode to check
      return false;
    }
  });  

})();

About

Fire an event after the user has finished typing

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages