Copy Events from One Element to Another
Need to clone an element and its events? Sure, you could rebind the events after doing the clone, but that wouldn’t be very DRY now, would it? Introducing Copy Events, a new plugin for jQuery.
Need to clone an element and its events? Sure, you could rebind the events after doing the clone, but that wouldn’t be very DRY now, would it? Introducing Copy Events, a new plugin for jQuery.
Here is a quick trick for getting an effect to delay without using setTimeout.
Let’s say, for example, that I want to show an alert message on the page every time a user clicks on a certain button. But I don’t want it to stay there forever; I want it to go away a few seconds later. You know, like the way they do in all of those crazy Web 2.0 sites.
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.
I’ve written a Fancy Letter Plugin that does all the hard work for you. You write a line of jQuery like $('div.content p').fancyletter(). The plugin wraps the first letter of the selected elements in a <span> with class names that you can then style to your needs.
Most of the code can remain the way we left it in Fancy Drop Cap – Part 2. We created a swap_letter() function to:
We also gave the image a class name of “fancy-letter” so that we could style it a bit:
I'm sure that some of you were expecting this entry to discuss traversing the DOM using jQuery methods. After all, I said I would do just that in my last entry. But a couple nights ago, I had the impulse to try a "live comment preview," so I'm going with it. My next entry, then, will be "How to Get Anything You Want - Part 2." I promise.
Please keep in mind as you read through this tutorial the "learning" part of this site's title: I'm learning jQuery just as many of those who will read this entry are. I mention this now because I have the nagging feeling that it was just too easy to create the live preview. Surely I must be doing something wrong. As always, I am counting on the superior knowledge of my readers to point out where my code fails. But enough of the apologies. Let's get started.
For the live preview I started with the default comment form in WordPress's Kubrick template:
The important part here is the textarea's id="comment". We want to show the contents of that textarea as the user inputs them.
Now on to the jQuery code. Read the rest of this entry »
In Fancy Drop Cap - Part 1, I showed how I used jQuery to insert a drop cap on my personal weblog. But there is still some unfinished business to take care of:
<a href="...">, <em>, etc.) So let's begin with item 1. As you may recall, we defined three variables, first_paragraph, first_letter, and text. The variables allowed us to get the value of the textNode of the first letter of the first paragraph, so we could replace it with the image. Here is what that part looked like:
The only problem with that code is that line 3 assumes that the first child node of the first paragraph is actually a text node. But what if it's a span tag (<span>) or a link (<a href="...">)? Well, in that case we'll need to keep drilling down through the nodes until we can't go any farther.
To do that, we'll set an intermediate variable, called node, initially making it the same as first_paragraph:
Next, we change our node variable to be defined as the first child of that node (node = node.firstChild), and we keep doing that until there are no more child nodes left, by using a "while" loop:
So, in other words, as long as there is a child node, our variable will be reset as that child node.
When that's all done, we set our text variable, this time as the value of our node variable:
Now all we have to do is get the first letter of the text so that we can replace it with the drop-cap image: var first_letter = text.substr(0,1).
There is just one more thing that we should account for Read the rest of this entry »
Last spring when I implemented a new design for my weblog, I wanted to use a fancy drop cap for the first letter of the first paragraph of the first post of each page. There are all sorts of ways to make a drop cap happen, but since I was reading Jeremy Keith's excellent book DOM Scripting at the time, I thought I'd do it that way. The DOM scripting method that I put together had some important benefits for me at the time:
Now that I'm learning jQuery, I thought I'd revisit my code and see if I could tidy it up a bit, the jQuery way.
I first put together a set of images, one for each letter of the alphabet, using a font from the Dieter Steffman collection at typOasis. If you don't want to go through the laborious process of converting letters into images, you can download mine (20KB zip). See the letter "A" floating to the right of this paragraph for an example.
Instead of putting all of the code in a $(document).ready() function, I created a separate function and just called it inside $(document).ready():
Now we can get down to business.
The easiest part of the process was inserting the image, because jQuery makes it almost effortless. Read the rest of this entry »
© Copyright 2006–2012 Learning jQuery and participating authors. Written content on this site is under a Creative Commons License. Code examples are under your choice of MIT or GPL license.
Development by Karl Swedberg. Design by Rex Rainey. Published with WordPress.