Home | Blog | Twitter @AndyJ | Contact Me | Snippets/Downloads | RSS

Submit jQuery tabs back on to itself

jQuery UI is a powerful set of interaction plugins for building RIA apps that run and the look the same across multiple browsers. Below is an example of using the tabs to hold multiple forms which submit back to themselves. The code is pretty straight forward and consists of two pages, the first one can even be a HTML page but the second one in my demo is a ColdFusion page to show the passed variables.
   view plainprintabout
 <html>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <title>Submit jQuery tabs back on to itself</title>
 <!-- Use Google CDN for jquery files -->
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
 <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js" type="text/javascript"></script>
 <!-- You can use Googles CDN for the themes as well -->
10  <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/swanky-purse/jquery-ui.css" type="text/css" />
11  <script>
12  $(document).ready(function(){
13  // set the tabs
14  $("#tabs").tabs();
15  // Capture the form's submit event
16  $('.inlineForm').live('submit', function() {
17  $.ajax({ // create an AJAX call...
18  data: $(this).serialize(), // get the form data
19  type: $(this).attr('method'), // GET or POST
20  url: $(this).attr('action'), // the file to call
21  success: function(response) { // on success..
22  $('.ui-tabs-panel:visible').html(response); // update the DIV
23  }
24  });
25  return false; // cancel original event to prevent form submitting
26  })
27  }); // END (DOCUMENT).READY
28  </script>
29  </head>
30  <body>
31  <div id="tabs">
32  <ul>
33  <li><a href="#tabs-1">Form 1</a></li>
34  <li><a href="#tabs-2">Form 2</a></li>
35  </ul>
36  <div id="tabs-1">
37  <!-- This is your first form. You could call this content in from Ajax?
38  Also note the class name which is the reference point for jQuery
39  -->

40  <form action="receive.cfm" method="get" class="inlineForm">
41  <input type="text" name="val1" value="" />
42  <input type="submit" name="submit" value="Submit Form 1" />
43  </form>
44  </div>
45  <div id="tabs-2">
46  <!-- This is your second form. You could call this content in from Ajax? -->
47  <form action="receive.cfm" method="get" class="inlineForm">
48  <input type="text" name="val2" value="" />
49  <input type="submit" name="submit" value="Submit Form 1" />
50  </form>
51  </div>
52  </div>
53  </body>
54  </html>

The second page should be called receive.cfm

   view plainprintabout
 <!-- This would process your form information and pass back a result to the user -->
 <cfsetting showdebugoutput="false" />
 <script>$("#dialog").dialog( {bgiframe: true, height: 140, modal: true} );</script>
 <div id="dialog" title="Done!">
 <p>Form submitted in to same tab</p>
 </div>
 <p>
 <cfdump var="#url#" format="text" label="Url variables">
 </p>

Pet Rescue SOS, a new site in the works

Pet Rescue SOS is a site I've had a hand in putting it together, put simply, is the place where you can register your pets, have them assigned a unique tag and identity number which can used to identify and locate your pet anywhere in the world. This service is available online 24hrs a day and you have access to your own registration section from where you can manage your account and pets.

Below for anyone interested is some of the tech specs:

Checking for a DIV on a page with jQuery

Trying to find if a DIV is present on a page with jQuery
   view plainprintabout
 if($('#DIVID').length){
  /*DIV isDefined()*/
 }
Was thinking of putting this in a jQuery plugin like isDefind() put then its not worth the extra code I think :)

My blog has moved

Please update your bookmarks and feeds for my site.

I now have a Mango Blog at:

http://www.andyjarrett.com/blog

Feed URL: http://feeds.feedburner.com/andyjarrett

Creating a new DOM element with jQuery

I've been using jQuery for a while now keep meaning to blog about the little bits I use often. One is creating a DIV object on the fly and loading content in to it.
   view plainprintabout
 $(document).ready(function(){
     // Your URL of the page to be loaded
     u = "/index.cfm?event=contactme";
 
     // We're creating a div element to load the URL contents into
     div = $("<div>");
 
     // When the element is displayed at first we could leave it blank
     // or a loading image, or in this case the wording loading......
10      div.html("Loading......");
11  
12      // We now load our file and inject it into the DOM.
13      div.load(u);
14  
15      // With the DIV getting its content we need to put the DOM on the page
16      // and in to the body. $.prepend will put our content at the top of
17      // of the <body>
18      $("body").prepend(div);
19  })
Instead of having "Loading...." being displayed you can delay showing the content of the DIV until loading is completed.
   view plainprintabout
 // Your URL of the page to be loaded
     u = "/index.cfm?event=contactme";
 
     // We're creating a div element to load the URL contents into
     div = $("<div>");
 
     // We now load our file and inject it into the DOM.
     div.load(u);
     
10      // By default we're going to hide the DIV until the content is loaded
11      div.hide();
12  
13      // Before the DIV gets its content we need to put the DOM on the page
14      // and in to the body. $.prepend will put our content at the top of
15      // of the <body>. But this will be hid.
16      $("body").prepend(div);
17  
18  
19      // Now we load the content into the DOM and as a CALLBACK function
20      // we SLIDEDOWN the div. This could be FADEIN or something else.
21      div.load(u, {limit: 25}, function(){
22          div.slideDown();
23      });
Both sets of code will load on document ready. Usually I tie this kind of process to a click event to load in a page dynamically when needed. Also don't forget you can chain these methods too and cut down on code. Taking the first example it could be written as:
   view plainprintabout
 u = "/index.cfm?event=hermes.contactSeller";
     div = $("<div>").html("Loading......").load(u);
     $("body").prepend(div);

jQuery FAQ Expand/contract

I've seen some code out there for this, but I've not managed to Google anything I like. My version uses HTML's Definition Lists. <DL>, <DT> and <DD>. Click on one of the questions below to see it in action. N.B. Thanks to Peter Boughton's comment. Though I try using next() and it wasn't working for me, hence the ID hack I did have. Anyway, thanks Pete.
Question 1
FAQ Answer 1: Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nam malesuada urna non ipsum. Integer quis pede. Nulla tincidunt. Sed venenatis semper justo.
Question 2
FAQ Answer 2: Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nam malesuada urna non ipsum. Integer quis pede. Nulla tincidunt. Sed venenatis semper justo.
Question 3
FAQ Answer 3: Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nam malesuada urna non ipsum. Integer quis pede. Nulla tincidunt. Sed venenatis semper justo.
Question 4
FAQ Answer 4: Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nam malesuada urna non ipsum. Integer quis pede. Nulla tincidunt. Sed venenatis semper justo.
Question 5
FAQ Answer 5: Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nam malesuada urna non ipsum. Integer quis pede. Nulla tincidunt. Sed venenatis semper justo.
Here's the code. I've put remarks in the comments on how it works
   view plainprintabout
 <script src="/jquery/jquery-1.2.6.min.js" type="text/javascript"></script>
 
 <script type="text/javascript">
     $(document).ready(function(){
         $("dt").click(function () {
             $('dd:visible').slideUp('fast');
             $(this).next('dd').slideDown('fast');
         });
     });
10  </script>
11  
12  
13  <style>
14      /* Hide all DD's at the start*/
15      dd{display:none;}
16  </style>
17  
18  <!-- Some FAQ Questions -->
19  <cfoutput>
20  <dl>
21      <cfloop from="1" to="5" index="i">
22          <dt class="q#i#">Question #i#</dt>
23          <dd id="q#i#">FAQ Answer #i#: Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nam malesuada urna            non ipsum. Integer quis pede. Nulla tincidunt. Sed venenatis semper justo.</dd>
24      </cfloop>
25  </dl>
26  </cfoutput>

jQuery, it really is simple

I've started playing around with jQuery along with the rest of the world it seems and all I can say is WOW. Working with it has been straight forwards and simple.

There's a couple of things out of the box that I like about it, but mainly the fact it comes as one .js file so doesn't matter what affect you want its there. In regards to it's simplness here some quick examples to get going:

[More]

BlogCFC / created by Raymond Camden / running version 5.9.5.003 / Contact AndyJarrett.com / Pet Rescue SOS www.redgiraffes.co.uk