stephan
stephan

Reputation:

jquery adds own events/functions to html elements of choice?

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?!

Upvotes: 0

Views: 796

Answers (1)

kgiannakakis
kgiannakakis

Reputation: 104188

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.

Upvotes: 5

Related Questions