Professional Documents
Culture Documents
01. $('<a></a>', {
02. text: 'jQuery is awesome!',
03. href: 'http://www.jquery.com',
04. id: 'someAwesomeID',
05. rel: 'external',
06. title: 'This is a title'
07. }).appendTo('body');
01. jQuery('#foo').bind({
02. click: function() {
03. // do something
04. },
05. mouseover: function() {
06. // do something
07. },
08. mouseout: function() {
09. // do something
10. }
11. })
AddyOsmani.com 1 of 9
4. Have you ever wanted to convert your JSON string into a
JavaScript Object? Rather than having to custom code your own
function to do it, just use jQuery's built in .parseJSON function
to achieve this easily (jQuery 1.4.1 and above):
view plain copy to clipboard print ?
5. Ever since jQuery 1.4 you've been able to use a jQuery feature
equivalent to PHP's sleep() called delay. If you would like to
delay and animated effect all you need to do is use delay as
follows:
view plain copy to clipboard print ?
01. $('#myJaw').slideUp(300).delay(800).fadeIn(400);
02.
7. Here's how you can remove the parent elements of any object
using jQuery's .unwrap() function
view plain copy to clipboard print ?
AddyOsmani.com 2 of 9
8. Would you like to perform an action when an element or its
contents gain focus? Here's how to do it:
view plain copy to clipboard print ?
01. jQuery('form')
02. .focusin(function(){
03. jQuery(this).addClass('focused');
04. });
05. .focusout(function(){
06. jQuery(this).removeClass('focused');
07. });
08.
09. //However, the preferred way to do this is using
10. // .focus() and .blur()
11.
12. //For when an element loses it's focus use .blur()
13. $('#target').blur(function() {
14. alert('Handler for .blur() called.');
15. });
16.
17. //For when an element gains focus use .focus()
18. $('#target').focus(function() {
19. alert('Handler for .focus() called.');
20. });
10. I've mentioned this a few times before, but it's a feature that
many developers forget exists within jQuery - data() - jQuery
actually has an API for invisibly storing data on DOM nodes so
you can easily store any information through JS and it'll be
available through the same central resource
view plain copy to clipboard print ?
AddyOsmani.com 3 of 9
12. Have you ever tried writing a plugin and run into a problem
with overriding specific behaviours?. In jQuery you can override
the values set or retrieved on the data method by binding to
getData and setData.Returning a value will set/return a totally
different result.
view plain copy to clipboard print ?
01. /*
02. Why namespace? Namespacing ensures that your
03. variables don't conflict with any others which
04. happen to have the same name. This is important
05. if you want to avoid your code breaking when
06. other files or plugins are included in your
07. page's architecture. See below for an example of
08. namespacing data.
09. */
10.
11. $("div").data("events.plugin",
12. {
13. //your data here
14. });
15. You can bind to almost any event and there aren't really any
limits on what you can or can't bind to.
view plain copy to clipboard print ?
AddyOsmani.com 4 of 9
16. A good tip for complicated applications: Custom events are a
useful way for multiple pieces of code to bind to the same
functionality, so you trigger one event but lots of handlers can
be executed.
view plain copy to clipboard print ?
17. Have you been trying to find a way to listen to events from a
particular context? The delegate and undelegate methods make
this possible (supported in jQuery 1.4.2 onwards). There's also a
very large performance gain got from switching over to
.delegate()!
view plain copy to clipboard print ?
18. You can dynamically change the context of a function (if needed)
using something similar to this. Since jQuery 1.4.* you've also
been able to easily remove the proxied function.
view plain copy to clipboard print ?
20. Did you know that you can treat jQuery objects like arrays?.
Take a look at this example.
view plain copy to clipboard print ?
AddyOsmani.com 5 of 9
view plain copy to clipboard print ?
01.
02. // returns 4
03. $('#wrapper .box').length;
04.
05. // num is equal to 4
06. var num = $('#wrapper .box');
07. num = num.length;
08.
09. // text is equal to 'Content #2!'
10. var text = $("#wrapper .box")[1];
11.
12. // text is equal to 'Content #1'
13. var text = $("#wrapper .box")[0];
14.
15. // text is equal to 'Content2 #1'
16. var text = $("#wrapper2 .box")[0];
21. Selection storage: Did you know that you can store the results
from a jQuery selection in a variable and *still* have access to
the same methods?. Here's a useful example.
view plain copy to clipboard print ?
22. Did you know that jQuery has great support for Callbacks? Here
are two ways you can tell if a function has completed running.
view plain copy to clipboard print ?
23. Very useful tip: Did you know that you can select elements
within another element just by passing a second parameter to
the jQuery initializer?
view plain copy to clipboard print ?
01. $('#yourparent').find('#mychild')
02. //or even shorter:
03. $('#mychild', $('#yourparent'))
04. //or even shorter:
05. $('#mychild', '#yourparent')
24. How do you handle access to elements with IDs that contain
special characters in jQuery?
view plain copy to clipboard print ?
AddyOsmani.com 6 of 9
25. How do you enable or disable form elements within jQuery?
view plain copy to clipboard print ?
26. How do you check if something exists in the DOM using jQuery:
view plain copy to clipboard print ?
27. How do you search all the nodes on a page for a particular piece
of text using jQuery?
view plain copy to clipboard print ?
AddyOsmani.com 7 of 9
28. Have you ever wanted to retain any of the information
.remove() strips from the DOM? The .detach() method is a lot
like remove() except that .detach() keeps all jQuery data
associated with the removed elements. This is useful when you
want to reinsert removed elements into the DOM later.
view plain copy to clipboard print ?
29. You can easily find the closest element to another (beginning at
the current element and moving up the DOM) using .closest().
See the below example:
view plain copy to clipboard print ?
01. $(document).ready(function()
02. {
03. //Let's set the background color of the nearest
04. //UL in this pseudo‐menu
05. $('li.subchild').closest('ul').css('background‐color', 'red');
06. });
01. <ul> <li>Parent Menu <ul> <li class="subchild">Child Item 1</li> <li class="subchild">Child Item 2
30. How to easily and quickly add one-click clearing of default input
text values from your fields
view plain copy to clipboard print ?
01. (function($){
02. $.fn.clearDefault = function(){
03. return this.each(function(){
04. var default_value = $(this).val();
05. $(this).focus(function(){
06. if ($(this).val() == default_value)
07. $(this).val("");
08. });
09. $(this).blur(function(){
10. if ($(this).val() == "")
11. $(this).val(default_value);
12. });
13. });
14. };
15. })(jQuery);
16.
17. // How to use it: $('input').clearDefault();
AddyOsmani.com 8 of 9
31. Would you like to style only a particular range of elements
within a list? jQuery's .slice() function makes this possible
through indices
view plain copy to clipboard print ?
- Addy
AddyOsmani.com 9 of 9