Tutorial Check if jQuery is Loaded
if (typeof jQuery == 'undefined') {
// jQuery IS NOT loaded, do stuff here.
}
if (typeof jQuery == 'undefined') {
// jQuery IS NOT loaded, do stuff here.
}
$('button').click(function(event, wasTriggered) {
if (wasTriggered) {
alert('triggered in code');
} else {
alert('triggered by mouse');
}
});
$('button').trigger('click', true);
Replace the first selector with the child you are testing and the second selector with the parent you are testing for.
if ( $(".child-element").parents("#main-nav").length == 1 ) {
// YES, the child element is inside the parent
} else {
// NO, it is not inside
}
if ($('#myElement').length > 0) {
// it exists
}
Or to make it a fancy function with a callback:
// Tiny jQuery Plugin
// by Chris Goodchild
$.fn.exists = function(callback) {
var args = [].slice.call(arguments, 1);
if (this.length) {
callback.call(this, args);
}
return this;
};
// Usage
$('div.test').exists(function() {
this.append('<p>I exist!</p>');
});
Say that 10 times fast =).
Find out if a single checkbox is checked or not, returns true or false:
$('#checkBox').attr('checked');
Find all checked checkboxes:
$('input[type=checkbox]:checked');
Do something for each empty element found:
$('*').each(function() {
if ($(this).text() == "") {
//Do Something
}
});
TRUE or FALSE if element is empty:
var emptyTest = $('#myDiv').is(':empty');
If you don't set the wmode on a flash embed it will float over the top of an overlay which can be a pretty big deal. This is ideal in environment with lots of legacy video code or where users will be posting new code and teaching them about wmode is a lost cause. This should work in all browsers.
$("embed").attr("wmode", "opaque");
var embedTag;
$("embed").each(function(i) {
embedTag = $(this).attr("outerHTML");
if ((embedTag != null) && (embedTag.length > 0)) {
embedTag = embedTag.replace(/embed /gi, "embed wmode="opaque" ");
$(this).attr("outerHTML", embedTag);
} else {
$(this).wrap("<div></div>");
}
});