21 Sep, 2009

Published at 06:17PM

Tagged with code, javascript, jquery, plugin, and programming

This post has 5 comments

Searchbox: jQuery live search plugin

Ah, the infamous live search. Often overused, but handy nonetheless. A project I’m currently working on has several places where a live search is exactly the right solution. I looked into the existing options, but wasn’t quite satisfied with the implementations. Since it’s an easy enough thing to do, I wrote a plugin for it. It’s called searchbox and can be found on github.

Usage

In it’s basic form (if you’re happy with the default configuration):

1
$('input.search').searchbox()

Or you can configure a few things:

1
2
3
4
5
6
7
$('input.search').searchbox({
  url: '/your/search/url',
  param: 'q',
  dom_id: '#thumbnails',
  delay: 250,
  loading_css: '#spinner'
})

See the implementation (or the README) for the defaults.

Hooks

There are currently three places you can hook into the plugin.

  1. initialization: init.searchbox
  2. before the search is submitted: before.searchbox
  3. after the search is completed: after.searchbox

These events get triggered at the right time and place within the plugin, and don’t do anything by default. But if you need to execute some special code in one of those three situations, you just have to bind a function to any one of those events. For example, to execute some code after the search:

1
2
3
$(document).bind('after.searchbox', function() {
  $('a[rel=facebox]').unbind('click').facebox()
})

Feel free to fork and improve.

Comments

mark Wednesday, 04 Aug, 2010 Posted at 07:31PM

do you have demo page for this?

Ryan Wednesday, 04 Aug, 2010 Posted at 08:30PM

Mark

Sorry, no I don’t have a demo page. The idea is to have the URL hit the server and return some data, and I didn’t want to always have to have that URL working for a demo page.

But it really is pretty simple. The README has the goodies, which I’m sure you’ve seen already.

Let me know if you have any questions/issues getting it to work, I’d be happy to help out if I can.

Gregg Turnbull Friday, 28 Jan, 2011 Posted at 02:25PM

I have been able to get an individual search box to work without fail, but when I add more than one, it appears it reads the last parameters every time searchbox is called. Am I missing something? left a message on github with code but can’t post that same code in the comment here. Thanks.

Ryan Tuesday, 01 Feb, 2011 Posted at 11:49PM

@Gregg -

Hey, sorry to leave you hanging, I’ve had a lot going on lately. That’s odd behavior. I’ve not needed it for multiple search boxes on the same page, so I can’t say. If I get time in the next day or two I’ll take a look.

tjeden Monday, 07 Feb, 2011 Posted at 11:27AM

@Greg

This plugin works only with one parameter. I’ve made a quick fork: https://github.com/tjeden/searchbox and now it serialize given form instead of one input field. I will work on it (i.e. this livesearch won’t work on select_fields) also I want to allow some customization (to choose wanted fields). Please give me info if you would like some other bahaviour.

Do you have something to say about this post?
Retype the image to the right Spam Hint: Are You Human? Textile Formatting Tips

or

Ryan Heath | Site Management A Ruby on Rails production.

This site is a Formed Function. Formed Function LLC | @formedfunction | Get in Touch