Halhelms
SIGN UP FOR MY NEWSLETTER
 
 
Halhelms

Shameless Money

Recent Entries

RSS

Subscribe

Learning jQuery, Day 3

Day 2 of this series showed how to use the CFAJAXPROXY tag along with jQuery to do sortable lists. Today, let's just see how to use jQuery to load content on the server -- both from a CFM file and a CFC.

We'll start with an HTML page called "formlesspage.htm":

<html>
   <head>
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
      <script type="text/javascript">
         $(document).ready(function(){
            $('#show_form').click(function(){
               $('#container_form').load('smallform.cfm');
            });
            $('#cancel').click(function(){
               $('#container_form').empty();
            });
         });
      </script>
   </head>
   <body>
      <p>Hello, welcome to our small, formless page. Would you like to add your name?</p>
      <input type="button" id="show_form" value="Sure!">
      <div id="container_form"></div>
   </body>
</html>

When the user clicks the "Sure!" button, we'll use jQuery to load the contents of the file, "smallform.cfm", into the div, "container_form".

Here's the contents of "smallform.cfm":

<form action="process_smallform.cfm" method="post">
   <div id="container_firstName">
      <label for="firstName">First Name</label>
      <input type="text" name="firstName" id="firstName">
   </div>
   <div id="container_lastName">
      <label for="lastName">Last Name</label>
      <input type="text" name="lastName" id="lastName">
   </div>
   <input type="button" id="cancel" value="Cancel">
   <input type="submit" value="OK">
</form>

Now, if you try that out, you'll see it works great!

Well, almost.

If you click the "Cancel" button, nothing happens. But in our "formlesspage.cfm", we have jQuery code to handle the cancel button being clicked. So why doesn't it work?

Let's look at the one that works: our "Sure!" button. The code...

$('#show_form').click(function(){
...code here...
});
...attaches an anonymous function to the DOM "click" event. That event is fired automatically by the browser when the user "clicks" the button.

Similarly, jQuery attempts to attach an anonymous function as an event handler when the DOM element with an id of "cancel" is clicked. But when the page loads, there is no such element. jQuery doesn't throw an error since there is no error.

But, of course, that's not the behavior we're looking for. Prior to version 1.2 (I think) of jQuery, this caused developers to have to think about something called "event delegation". I won't get into this because...

  • you would have to understand how something called "event bubbling" occurs
  • you would have to understand how event bubbling is handled by different browsers
  • you don't have to worry about this anymore!

With the latest version of jQuery,the "live" method was introduced. Let's use it to solve this problem. I'll change the code for the "cancel" button:

$('#cancel').live('click', function(){
$('#container_form').empty();
});

Now, even though the code with the cancel button is loaded after the initial jQuery code runs, the event handler will be attached.

As for using a CFC instead of a .cfm file, something like this will work:

<cfcomponent displayname="SmallForm" output="false">
   <cffunction name="display" access="remote" output="true">
      <form action="process_smallform.cfm" method="post">
         <div id="container_firstName">
            <label for="firstName">First Name</label>
            <input type="text" name="firstName" id="firstName">
         </div>
         <div id="container_lastName">
            <label for="lastName">Last Name</label>
            <input type="text" name="lastName" id="lastName">
         </div>
         <input type="button" id="cancel" value="Cancel">
         <input type="submit" value="OK">
      </form>
   </cffunction>
</cfcomponent>

jQuery's introduction of the "live" method is an example of how John Resig and his excellent team work hard to make programmers' lives a bit easier. Grazi, John.

Comments
John Farrar's Gravatar EEEK!!! You are using the dreaded $ symbol. That is typically safe but there is a better way where code breaks less often. You should make it a point to teach people to build domain safe code by shutting down the external reference to the $ symbol. It can be turned on again inside if people like this. ESPECIALLY if you build a plugin to share with others MAKE SURE you do not depend on the $ reference. :)
# Posted By John Farrar | 3/31/09 10:50 AM
Hal's Gravatar The dreaded $ symbol? Well, when writing plugins, yes, it's better to use "jQuery" (which the $ is a shortcut to), but in my own code, I use the $ extensively.
# Posted By Hal | 3/31/09 10:56 AM
Michael Brennan-White's Gravatar How would you code the load statement if you are loading the cfc rather than the cfm in the segment $('#container_form').load('smallform.cfm');

I'm sure you covered it somewhere but I missed it.
# Posted By Michael Brennan-White | 4/1/09 12:22 PM
Hal's Gravatar @Michael: It would be $('#container_form')
.load('SmallForm.cfc?method=display');
# Posted By Hal | 4/1/09 12:44 PM
 
   
Clicky Web Analytics