As mentioned in a recent post, I mentioned that I have really been enjoying the book, "jQuery Enlightenment". Another thing I learned while reading this book was how you can create a custom event on an element (standard events being events like click, mouseover, etc.). Something seemed really cool about custom events - I'm not exactly sure how I would use them, but I know that I want to remember them, which is why I'm posting this.
If I had a span element, I could use a custom event to make it alert a message. (True, I could just use a click event, but like I said, I'm sure down the road I will have a good use for this).
Here's how to assign the event with jQuery:
$('span').bind('myOwnEvent', function(){
alert("My Own Event works!");
});
Then, to execute (or trigger) the custom event, use jQuery again:
$('span').click(function(){
$(this).trigger('myOwnEvent');
});
What I am not clear on right now is that if this is possible with native JavaScript or not - I like to know the "JavaScript theory" behind the jQuery methods - I'll have to do some research on that.
I'll end this post like my last one:
I am learning so much from this book, "jQuery Enlightenment". I thought I knew a bit about jQuery, but this book has helped me learn so much more. I highly recommend you go to the site and buy it. Thanks Cody!
If I had a span element, I could use a custom event to make it alert a message. (True, I could just use a click event, but like I said, I'm sure down the road I will have a good use for this).
Here's how to assign the event with jQuery:
$('span').bind('myOwnEvent', function(){
alert("My Own Event works!");
});
Then, to execute (or trigger) the custom event, use jQuery again:
$('span').click(function(){
$(this).trigger('myOwnEvent');
});
What I am not clear on right now is that if this is possible with native JavaScript or not - I like to know the "JavaScript theory" behind the jQuery methods - I'll have to do some research on that.
I'll end this post like my last one:
I am learning so much from this book, "jQuery Enlightenment". I thought I knew a bit about jQuery, but this book has helped me learn so much more. I highly recommend you go to the site and buy it. Thanks Cody!
No comments:
Post a Comment