Return to Snippet

Revision: 23909
at February 16, 2010 15:47 by vagrantradio


Initial Code
//Remove a word with jQuery
var el = $('#id');
el.html(el.html().replace(/word/ig, ""));
 
//jQuery timer callback functions
window.setTimeout(function() {
 $('#id').empty();
}, 1000);
 
//Verify that an element exists in jQuery
if ($('#id').length) {
 // do stuff
}
 
//Dynamically adding <div> elements with jQuery
$('<div>hello<\/div>').appendTo(document.body);
 
//Searching for Text with jQuery
//This function performs a recursive search for nodes that contain a pattern. Arguments are //a jQuery object and a pattern. The pattern can be a string or a regular expression.
$.fn.egrep = function(pat) {
 var out = [];
 var textNodes = function(n) {
  if (n.nodeType == Node.TEXT_NODE) {
   var t = typeof pat == 'string' ?
    n.nodeValue.indexOf(pat) != -1 :
    pat.test(n.nodeValue);
   if (t) {
    out.push(n.parentNode);
   }
  }
  else {
   $.each(n.childNodes, function(a, b) {
    textNodes(b);
   });
  }
 };
 this.each(function() {
  textNodes(this);
 });
 return out;
};
 
//Case-Insensitive Contains Check
//By using case-insensitive regular expressions, this becomes rather easy:
var pattern = /exp/i;
$('#id').filter(function() {
 return pattern.test($(this).text());
});
 
//Reload an IFrame
//Accessing the contentWindow property to obtain the location object.
$('#iframe').each(function() {
 this.contentWindow.location.reload(true);
});
 
//Callback for IFrame Loading
//With the onload event, your code is called when an IFrame has finished loading.
$('#iframe').load(function() {
 // do something
});

Initial URL


Initial Description
Useful random jQuery snippets.

Initial Title
RANDOM JQUERY SNIPPETS

Initial Tags
jquery

Initial Language
jQuery