jQuery Topics

Marco Polo, a jQuery autocomplete plugin

Today, we have a Guest Post by Justin Stayton. He talks about his new developments including Marco Polo, a jQuery autocomplete plugin and Manifest, a jQuery plugin that adds delight to selecting multiple values for an input.

Let me Google something for you: “jQuery autocomplete plugin”. Over the past four years, I’ve Googled this exact phrase north of a dozen times. The results, however, really haven’t changed that much: there are a lot of options, but none that spark enough confidence in me to rely upon in a production web app.

I’m very picky when it comes to the jQuery plugins I choose. If my users have issues with a plugin, that falls on my plate. I really don’t want to worry about third-party code in my system, so I’ve developed something of a litmus test that a jQuery plugin must pass to garner my consideration. In no specific order: the plugin must be well documented and code-commented, maintained with an update in the past six months, have very few (if any) known issues, follow modern jQuery coding patterns, and be flexible enough to fit 80% of my needs without a myriad of additional features that I’ll never use. None of the jQuery autocomplete plugins I’ve tried over the past four years have ever passed this test.

Marco Polo

marco-polo

So, I decided to write my own. Meet Marco Polo, a jQuery autocomplete plugin for the discerning developer. I think you’ll like it, especially if you’re as selective as I am in choosing jQuery plugins. But, don’t just take my word for it: play with the live examples, read through the documentation, and try implementing Marco Polo within your own system. Here’s a quick overview of how that’s done.

Demos

Getting Started

To start, make sure both jQuery and Marco Polo are included in your HTML:

<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="jquery.marcopolo.min.js"></script>

Next, add a text input, if you haven’t already:

<input type="text" name="userSearch" id="userSearch" />

Then attach Marco Polo to the text input in your JavaScript:

$('#userSearch').marcoPolo({
  url: '/users/search',
  formatItem: function (data, $item) {
    return data.first_name + ' ' + data.last_name;
  },
  onSelect: function (data, $item) {
    window.location = data.profile_url;
  }
});

When a search happens, a GET request is made to the url with q (the search value) added to the query string. Let’s say a search is made for Butler. A GET request is made to /users/search?q=Butler. Your backend code must then use the q parameter to find and return the matching users in JSON format:

[
  {
    first_name: 'James',
    last_name: 'Butler',
    profile_url: '/users/78749',
    …
  },
  {
    first_name: 'Win',
    last_name: 'Butler',
    profile_url: '/users/41480',
    …
  },
  …
]

Each JSON user object is passed to the formatItem callback option for display in the results list. And when a user is selected from the results list, their JSON object is then passed to the onSelect callback option to complete the browser redirect.

That’s it! While this example demonstrates a number of fundamental concepts, the possibilities extend far beyond the straightforward search, click, redirect setup shown here. Try it out yourself, and I think you’ll be surprised at just how flexible Marco Polo can be.

Manifest

maifest
A jQuery plugin that adds delight to selecting multiple values for an input. The recipients field in an email app is a perfect example. You could just offer a plain text input, requiring the user to manually separate each recipient with a comma. Removing a recipient, however, is a pain: the user has to precisely select just the right amount of text, making sure not to accidentally remove too much or too little. As a discerning developer, you know the user experience should be better. And it can be, with Manifest.
Demos

About the Author

justin-stayton
Justin Stayton is the Lead Developer at Monk Development, Austin, Texas. You can catch him on GitHub and Twitter@jstayton.

Read the full/original article at jQuery4u

    Related Posts

    Comments are closed.

    jQuery Resources

    Learn more about jQuery

    jQuery News & Links

    I only just found out that the jQuery validation plugins has a validation rule called “remote” which can be used ...

    jQuery function to check if horizontal scroll is present – hasHScrollBar() – (or vertical check below also, util function to ...

    jQuery function to Set any DOM Element to Top View (bring to front) using CSS Z-Index property.

    Is it possible to declare arrays in JavaScript object literal notation? Example 1 – this works with arrays Declaration: Storage ...

    To get the a variables type using jQuery there is a jQuery function called .type() which returns “array”, “string”, “number”, ...

    In this post you can find tutorials which explain step by step different API use cases with jQuery like Google ...

    For valuable work on creation of sites you need a good comfortable editor necessarily. There are many requiring paid products ...

    Today we are sharing with you a collection of awesome jQuery Camera Photo plugins. They offer a range of image ...

    Back in the day, if you saw something that was animated on a website it was automatically assumed to be ...

    In this post, we have compiled a list of 10 jQuery HTML5 Audio players available today, most allow native audio ...

    jQuery Mobile is a powerful framework for making mobile web applications. But can we use it to convert existing desktop ...

    I have jotted a quick post on a Basic JavaScript Regular Expression Example to give beginners out there a taste ...

    In April 2012′s edition of Interestingly Random JavaScript, jQuery and Web Development we bring you some very cool stuff such ...

    In this post we are sharing you a roundup of 10 really jQuery plugins as of today. Pretty cool plugins ...

    As always expected from the jQuery community, the compilation we have for you today are some good and impressive recently ...

    A collection of jQuery PNG/JPEG/GIF plugins that enables image animation, cartoon-like background, etc… let them help you design displays for ...

    A collection of JavaScript/jQuery Zip/File/Compressor plugins that allows you to minify your JS code and compress your JS files ready ...

    Quick jQuery code snippet on how to keep an element in view. For demo scroll down on any page on ...

    Here is what I think is the best and most reliable way to load jQuery library and jQuery UI Libraries. ...

    jQuery code snippet which outputs all attributes of element using the default .attr() function on any dom element(s). usage: output:

    More Links & News

    Community Resources

    Use jQuery and the Google Maps API to create your own map application.

    Super cool and easy way to get stylish tooltips with jQuery.

    The right way to include the jQuery library in WordPress.

    Share a resource