How to create custom event in jQuery

Hi I would like to describe a script that I would like to execute, and I have no idea how

I would like to bind \ live on an element to a custom 'myclick':

$('#somediv').bind('myclick', function() {});

obviously, I would like to trigger this event only when something happens to this element, for example if myclick happened on this element

I know how to fire a custom event for a specific item ( http://api.jquery.com/category/events/event-object/ )

Clarifications to the question:
I will determine what myclick means (for example, 3 consecutive clicks in this element)
how can I tell when this happened on "somediv"?
Am I somehow registering through jquery elements that bind \ live to them? if so, how?

full example:

<html>
<head>
   <script to include jquery>
   <script to include MYCODE.js>

   <script>
     $('#somediv').bind('myclick', function() { alert('myclick'); } );
   </script>

   <body>
      <div id='somediv'></div>
   </body>
<head>
</html>

so my question is: what does MYCODE.js contain?
how can he say that some kind of user logic that he defines happened on "somediv"?

thanks for the help

+3
source share
3 answers

Again, I find myself answering my question after the wisdom of the masses has undermined me.
In any case, the answer is to write a jQuery plugin.

- http://blogs.sitepoint.com/how-to-develop-a-jquery-plugin/ http://starter.pixelgraphics.us/

, - .

http://code.google.com/p/jquery-touch-area/

Enjoy

0

.trigger() - , . DOM, .bind()

$('#somediv').trigger('myclick')
+2

In the same way, as Jeff says, you need to call $('#somediv').trigger('myclick');in any logic that should cause the event to fire. Your example binding in the original question looks right to me, so that should be all you need to do.

0
source

All Articles