/ Published in: jQuery
Useful random jQuery snippets.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
//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 });