QuackFuzed.com is the personal ColdFusion coding blog of Matt Quackenbush. It exists primarily as a place for the author to learn, and hopefully to assist others in learning and/or avoiding some of the same pitfalls and mistakes. (Quack certainly makes enough mistakes daily to make up for the entire ColdFusion community.)

jQuery DOM Manipulation

Posted on January 21, 2008 at 2:10 AM in General, jQuery

In case you hadn't heard yet, jQuery is a seriously bad-ass, lightweight library that I'm in love with. Here's what the official jQuery site has to say about it:

"jQuery is a fast, concise, JavaScript Library that simplifies how you traverse HTML documents, handle events, perform animations, and add Ajax interactions to your web pages. jQuery is designed to change the way that you write JavaScript."

However, you can always leave it to a guy like me to find a way to screw things up.

[Continue Reading]

Comments
(Comment Moderation is enabled. Your comment will not appear until approved.)

On 1/21/08 at 1:15 PM, Dan G. Switzer, II said:

@Matt:

This seems to be a pretty common misconception amongst developers new to jQuery. When execute methods against a jQuery object/selector, it only knows about DOM objects that are currently in the DOM.

You can think of this like running SELECT SQL statement in CFQUERY. If some records are added/changed *after* you run the query, ColdFusion won't be aware of the changes unless you re-run the query.

Personally, I've stayed away from the LiveQuery plug-in. Instead when I have logic that I know I'll need to attach to DOM elements in the future, I just use a helper function to attach the events to DOM elements. This gives me explicit control over what's happening and it doesn't add overhead of monitoring the DOM.

Lots of people have used LiveQuery and had great success, but controlling things manually is my preference.

Hopefully this post might help others to understand the problem in a little more detail.

On 1/21/08 at 2:56 PM, Matt Quackenbush said:

@Dan - Thanks for the great feedback. Care to offer a little snippet that demonstrates your helper function attaching the event(s) to the DOM element(s)? I'd be very interested in learning more, and I'm sure others would be as well.

As a developer, I most definitely understand wanting as much control as possible over your environment, but as a jQuery n00b, I find that I often have to give up some control to get things accomplished in a timely fashion. Just like when I was a CF n00b all those years ago, I used all sorts of tags that I wouldn't touch with a 10-ft pole today (e.g. cfform, cfinsert, cfupdate, etc).

On 1/21/08 at 3:09 PM, Dan G. Switzer, II said:

@Matt:

A simple example would be:

function clickMe(o){
o.bind("click", function (){ alert('click me'); }).show();
}

Now for any element you want to attach the clickMe event and make sure the element is visible, you could just do:

clickMe( $('div.clickme') );

That's a simplified example, but it easily allows you to perform the exact same logic/conditions to multiple jQuery objects.

On 1/21/08 at 4:46 PM, Josh Nathanson said:

This is probably the most asked question on the jQuery email list - "why don't my events trigger on newly inserted elements?" It catches pretty much everyone off guard at first.

I am with you there Dan as far as not necessarily using LiveQuery. I only use it if I have a particularly complex UI that may have several event handlers that need to be rebound upon DOM insertions. If I only have one or two such rebindings I will set up a binder function as in your example.
CodeBassRadio

Latest Articles

Eventually something really brilliant and witty will appear right here.

Calendar

January 2008
S M T W T F S
« Dec   Feb »
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    

Subscribe

Enter a valid email address.

The Obligatory Wish List