views:

226

answers:

1

is it possible to add to an element type or maybe a css selection an own event function?

something like:

$("a").bind("blub", function() { alert("aaa" + this); });

$("a").get(0).blub();

i want define some functions which are only available for some special elements eg.:

the <div class="myDivContainer">...</div> should have the function available:

$("myDivContainer").get(0).blub();

but on maybe:

$("myDivSeparator").get(0).blub();

this should not work, because it was not defined for it

is this possible?!

+5  A: 

You can create custom events with bind and trigger. See for example this question.

You must use the trigger method to call the custom event:

$("a").bind("blub", function() {});
$("a").trigger("blub");

Of course you can trigger the "blub" event only for elements that bind has been called before.

If you want to use blub as a method, then you need to modify/extend the jQuery itself.

kgiannakakis
don't understand - how i call the binded method?!
The trigger("blub") calls the binded method. Google for custom events with jQuery. You will find many useful articles.
kgiannakakis