tags:

views:

479

answers:

2

Hi everyone,

Trying to get comfortable with jQuery and I have encountered some sample code that I am having trouble understanding. It refers to the bind method and the way they use it. Here it is

$('textControl').bind(($.browser.opera ? 'keypress', 'keydown') + '.autocomplete', function(event) { code...});

I understand the selection of either keypress or keydown but what I dont understand is why they attach the .autocomplete on the even handler name?

if anyone can shed some light that would be great

Thanks

+11  A: 

Let me explain, step by step.

$('textControl').bind(

binding to textControl

($.browser.opera ? 'keypress', 'keydown')

tertiary expression returning keypress if the browser is opera, keydown otherwise.

'.autocomplete'

Autocomplete is a custom event the jQuery plugin adds to the keypress event object. So basically they just grouped it in to make it a bit more organized but it's a custom namespace event.

So in reality, the event is attached to keypress/keydown, but you can bind and unbind using the namespace, keeping the workspace organized.

Dmitri Farkov
thanks to your step by step clarification, makes a lot of sense now.
ca_in_UK
+5  A: 

This is a features of jQuery called Namespaced Evenets which is often used within Plugins as it allows an easy and quick way for a plugin to only manipulate it's own events.

duckyflip
Thanks for clarification and the category this falls under, very helpful
ca_in_UK