Recent Posts

Showing posts with label element. Show all posts
Showing posts with label element. Show all posts

"exists" function in jQuery

For verify the existence of an element in jQuery this is the piece of code:

jQuery.fn.exists = function(){return this.length>0;}

if ($(selector).exists()) {
    // Do something
}

Remove event handler in an element with jQuery

[2011 - jQuery 1.7 <]

With jQuery 1.7 onward the event API has been updated, .bind()/.unbind() are still available for backwards compatibility, but the preferred method is using the on()/off() functions. The below would now be,

$('#myimage').click(function() { return false; }); // Adds another click event
$('#myimage').off('click');
$('#myimage').on('click.mynamespace', function() { /* Do stuff */ });
$('#myimage').off('click.mynamespace');
In your example code you are simply adding another click event to the image, not overriding the previous one:

$('#myimage').click(function() { return false; }); // Adds another click event
Both click events will then get fired.

As people have said you can use unbind to remove all click events:

$('#myimage').unbind('click');
If you want to add a single event and then remove it (without removing any others that might have been added) then you can use event namespacing:

$('#myimage').bind('click.mynamespace', function() { /* Do stuff */ });
and to remove just your event:

$('#myimage').unbind('click.mynamespace');

Get all the styles associated with an element jQuery

A couple years late, but here is a solution that retrieves both inline styling and external styling:

function css(a) {
    var sheets = document.styleSheets, o = {};
    for (var i in sheets) {
        var rules = sheets[i].rules || sheets[i].cssRules;
        for (var r in rules) {
            if (a.is(rules[r].selectorText)) {
                o = $.extend(o, css2json(rules[r].style), css2json(a.attr('style')));
            }
        }
    }
    return o;
}

function css2json(css) {
    var s = {};
    if (!css) return s;
    if (css instanceof CSSStyleDeclaration) {
        for (var i in css) {
            if ((css[i]).toLowerCase) {
                s[(css[i]).toLowerCase()] = (css[css[i]]);
            }
        }
    } else if (typeof css == "string") {
        css = css.split("; ");
        for (var i in css) {
            var l = css[i].split(": ");
            s[l[0].toLowerCase()] = (l[1]);
        }
    }
    return s;
}
Pass a jQuery object into css() and it will return an object, which you can then plug back into jQuery's $().css(), example:

var style = css($("#elementToGetAllCSS"));
$("#elementToPutStyleInto").css(style);


Check the existence of an element in jQuery

jQuery.fn.exists = function(){return this.length>0;}

if ($(selector).exists()) {
    // Do something
}
This is in response to: Herding Code podcast with Jeff Atwood

How to: detect a click outside an element with jQuery

Attach a click event to the document body which closes the window. Attach a separate click event to the window which stops propagation to the document body.

$('html').click(function() {
//Hide the menus if visible
});

$('#menucontainer').click(function(event){
    event.stopPropagation();
});
Warning, if using this technique, be aware of the dangers of stopping propagation.