DOM Traversing Entries

Revealing Details with jQuery

Friday, January 4th, 2008

A week or so ago, someone posted a comment on one of my previous articles, asking if I could help her split up the textual content of an element, showing the first part and replacing the second with a link that, when clicked, would reveal the text. This behavior would appear in an FAQ using a definition list (<dl>), with each question contained in a <dt> and each answer contained in a <dd>. I soon realized that the solution would be rather involved, so I decided to create a new entry out of it rather than simply answer her question in another comment.

Here is the simple definition list structure that I’ll be using for the example:

(more…)

Questions and Answers from the List

Wednesday, December 19th, 2007

I’ve been feeling guilty lately about my lack of posts to this blog. But when I looked at my profile for the jQuery Google Group and discovered that for the past six months I’ve posted an average of 100+ times each month, well, I decided to give myself a break. Since I’m sure some people who stumble upon this blog aren’t subscribed to the Google group/mailing list, here are a few (edited) questions that have appeared there recently, along with my (edited) answers. I hope some of you find them helpful.
(more…)

Selecting Elements by Properties and DOM Expandos

Wednesday, March 21st, 2007

Sometimes it is very convenient to store information on an element by using custom properties (DOM Expandos). For example, I use DOM Expandos in my Mouse Wheel plugin to store an array of handler methods (_mwHandlers) along with a method (_mwHandler) to call all those handlers when the mouse wheel is used. Storing these methods on the element allows me to write less code, and manage scope easier. It also gives me a function reference for easy removal when needed.

Now, what if I needed to select all elements that have a mouse wheel handler on them?

(more…)

Quick Tip - Optimizing DOM Traversal

Saturday, December 16th, 2006

The topic of optimization has come up a number of times in the jQuery mailing list. jQuery’s DOM traversal is powerful and easy, but it can sometimes be slow as well. As with any JavaScript library or framework, the helper methods will be slower than the plain old JavaScript (p.o.j) methods. Nevertheless, if we keep in mind the jQuery’s speed hierarchy, the speed difference between jQuery and p.o.j. will often be negligible.
(more…)

Multiple Fancy Drop Caps

Tuesday, December 12th, 2006

After I wrote a couple entries (Fancy Drop Cap, Part 1 and Part 2) on creating a drop cap for the first paragraph in a DIV, a couple people asked how one would go about making the drop cap apply to every paragraph in a DIV.

Most of the code can remain the way we left it in Fancy Drop Cap - Part 2. We created a swap_letter() function to:

  1. find the first letter of the paragraph
  2. concatenate that letter with the rest of an image tag if it matches one of the letters in a regular expression
  3. strip the letter out of the paragraph since we want to replace it with the image
  4. prepend the image tag to the paragraph

We also gave the image a class name of "fancy-letter" so that we could style it a bit:

CSS:
  1. .fancy-letter {
  2.  float: left;
  3.  margin: -1px 2px 2px 0;
  4. }

So, what needs to change? Not much actually.

The first thing to do is remove the line that sets the "first_paragraph" variable: var first_paragraph = $('#main-content p')[0]. In its place we'll use jQuery's .each(), which takes the pain out of for loops. All of the work will be done inside this .each() method, starting with setting a variable for "each" of the paragraphs:

JavaScript:
  1. $('#drop-caps p').each(function(index) {
  2.   var paragraph = this;
  3.   . . .
  4. });

We set the paragraph variable as this instead of $(this) for the same reason that we had (in the earlier entries) put the [0] after $('#main-content p') instead of writing $('#main-content p:eq(0)'): because we need to work on the actual DOM node. Also, note that I changed the DIV's id from "main-content" to "drop-caps" for this example, but you can name yours whatever you want.

The rest of the code is pretty much the same, because all we really needed to do was get a different set of elements Here is the new code in all its glory:

JavaScript:
  1. $(document).ready(function(){
  2.   swap_letter();   
  3. });
  4.    
  5. function swap_letter() {
  6.   $('#drop-caps p').each(function(index) {
  7.     var paragraph = this;
  8.     var node = paragraph;
  9.     while (node.childNodes.length) {
  10.       node = node.firstChild;
  11.     }
  12.     var text = node.nodeValue;
  13.     var first_letter = text.substr(0,1);
  14.  
  15.     var match = /[a-zA-Z]/.test(first_letter);
  16.     if ( match ) {
  17.       node.nodeValue = text.slice(1);
  18.       $('<img />')
  19.           .attr('src','/images/alphabet/' + first_letter.toLowerCase() + '.gif')
  20.           .attr('alt',first_letter)
  21.           .addClass('fancy-letter')
  22.           .prependTo( paragraph );
  23.       first_letter = "";
  24.     }
  25.   });
  26. }

For an explanation of the other parts, please refer to Fancy Drop Cap - Part 2 and Fancy Drop Cap - Part 1. In the meantime, feast your eyes on the beauty of these fancy first letters.

This is the first paragraph. The "T" in "This" should be replaced by the drop-cap image. The image is floated left with a little padding to the right and the bottom.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

How to Get Anything You Want - part 2

Sunday, December 3rd, 2006


A couple weeks ago I wrote about traversing the DOM with jQuery's selector expressions to get any elements in the document (see How to Get Anything You Want - part 1). This time around, I'm going to focus on jQuery methods that provide even more ways to get elements. Some of these methods have a nearly identical counterpart among the selector expressions, but for the most part, the two ways of getting elements complement each other.
(more...)