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

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);
TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Dan G. Switzer, II's Gravatar @Andy:

You could further chain to one line:

var div = $("<div>").html("Loading......").load("/index.cfm?event=hermes.contactSeller").appendTo("body");

However, I normally would format that to something like:

// create div
var div = $("<div>")
// add loading message
.html("Loading......")
// load AJAX content
.load("/index.cfm?event=hermes.contactSeller")
// add DIV to body
.appendTo("body");

I'm sure the spacing won't show up correctly, but I tab indent the html(), load() and appendTo() methods. If I have a method that changes the chain, then I further intend lines under that and then would unident for each end() method.
# Posted By Dan G. Switzer, II | 3/16/09 8:25 AM
Ben Nadel's Gravatar I agree with Dan - chaining is cool, but good formatting with chaining is a must.
# Posted By Ben Nadel | 3/16/09 12:37 PM
BlogCFC / created by Raymond Camden / running version 5.9.5.003 / Contact AndyJarrett.com / Pet Rescue SOS www.redgiraffes.co.uk