6 quick jQuery tips: Text manipulation, timers and elements

Posted 2008-02-26 in JavaScript by Johann.

This is the first of several jQuery snippet collections.

Remove a word with jQuery

The simple way – using regular expressions:

var el = $('#id');
el.html(el.html().replace(/word/ig, ""));

Test it!

jQuery timer callback functions

Want to call a method after a certain timeout?

window.setTimeout(function() {
 $('#id').empty();
}, 1000);

Remove this element one second after clicking it.

If you want to call a task periodically, use the Timer plugin for jQuery.

Verify that an element exists in jQuery

Simply test the .length property. Bonus information: This is used in inc.

if ($('#id').length) {
 // do stuff
}

Is there an element with an id of “top”?

jQuery not working in IE 5.0 or 5.5?

jQuery does not support older Internet Explorer versions. To make sure your users do not see JavaScript errors, edit your jquery.js file as follows:

// Put this before the original jQuery code
if (!(window.showModelessDialog && !document.implementation)) {
 (function() {

// Original jQuery code goes here

// Put this after the jQuery code
 })();
}

How to use a plugin with jQuery?

jQuery plugins are included on the page after the main jquery.js file:

<script type="text/javascript" src="jquery-1.1.4.js"></script>
<script type="text/javascript" src="jquery.roflcopter-1.0.js"></script>
<script type="text/javascript" src="jquery.lolcode-2.4.js"></script>

This is the beginner’s version. The advanced version is copying all your JavaScript files into a single file and then compressing it with YUI and /packer/.

Dynamically adding <div> elements with jQuery

…or any other element of course.

$('<div>hello<\/div>').appendTo(document.body);

Append some text to this blog entry.

Subscribe

RSS 2.0, Atom or subscribe by Email.

Top Posts

  1. DynaCloud - a dynamic JavaScript tag/keyword cloud with jQuery
  2. 6 fast jQuery Tips: More basic Snippets
  3. xslt.js version 3.2 released
  4. xslt.js version 3.0 released XML XSLT now with jQuery plugin
  5. Forum Scanners - prevent forum abuse
  6. Automate JavaScript compression with YUI Compressor and /packer/

Navigation