JQuery – Samples

Posted: November 22, 2010 in JQuery
Tags:

1. Check class exist or not – if($(“#id”).hasClass(“narroww”))
2. Change element width/height (one or more “,” separated targets) – $(“#id1 .clsname, #id2 .clsname”).width(10px).height(10px);
3. Set css – $(“#id”).css(‘visibility’, ‘hidden’);
4. The difference between .css(‘height’) and .height() is that the latter returns a unit-less pixel value (for example, 400) while the former returns a value with units intact (for example, 400px). The .height() method is recommended when an element’s height needs to be used in a mathematical calculation.
5. Show/Hide elements based on class/id/tag –
 $(“#id”).show(); $(“#id”).hide();
 $(“a”).show(); $(“a”).hide();
 $(“.myclass”).show(); $(“.myclass”).hide();

Events –

If you are using JQuery 1.4+ then always use unbind before calling bind, otherwise events will fire more than once due to binding the same event multiple times. Performance will be bad in case if you have too many links on page and you are attaching events to them through JQuery bind method.

1. $(this).unbind(‘hover’); does not work. So use either $(this).unbind(‘mouseenter’).unbind(‘mouseleave’); or $(this).unbind(‘mouseenter mouseleave’);
2. To unbind all events use $(this).unbind(); unbind() doesn’t work with hardcoded inline events Ex: <div id=”some_div” onmouseover=”do_something();”>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s