Archive for September, 2011

JQuery filter

Takes a selector or a function to filter your selected elements.

$("*").filter(function() {
    return $(this).css('z-index') > 0 ? true : false;
}).each(function() {
    $(this).css("border", "1px solid red");
    console.log("z-index:" + $(this).css('z-index'));
});
Advertisements

Some debug code to isolate an element

This is great if you have layout issues and want to simplify things. Just add a selector for the only element you want on your page. In the following its page header.

var theElement = $(".page-header");
$("body > *").remove();
$("body").append(theElement);

Shortcut to function arguments

var ResetDefaultPasswordForm = function (options) {
    var options = options || {},
    editButton = options.EditButton || $(".edit-email-settings-button")[0],
    collapsibleArea = options.CollapsibleArea || $(".email-settings .collapsible")[0];

    $(collapsibleArea).hide();
}

Thanks to this post for the tip and all the other which I’m sure I’ll be posting examples of very soon.

Safest way to check an object exists in JavaScript

Had to note this down somewhere. How to tell if an object is null or undefined.

if (!! foo) alert('foo is defined and not-null');
if (! foo) alert('foo is undefined or null');

or if you want to test an method or value that might return NaN, 0, zero-length string, or of course false is not null you can use

if (typeof(SomeObject)!="undefined")
if(typeof(SomeObject)!="unknown")

And there is nothing wrong with the following if you don’t want to distinguish between null and undefined.

if(SomeObject != null)

Thanks to this post and it’s comments for all the tips.