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.
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
$(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......
div.html("Loading......");
// We now load our file and inject it into the DOM.
div.load(u);
// With the DIV getting its content we need to put the DOM on the page
// and in to the body. $.prepend will put our content at the top of
// of the <body>
$("body").prepend(div);
})
1$(document).ready(function(){
2 // Your URL of the page to be loaded
3 u = "/index.cfm?event=contactme";
4
5 // We're creating a div element to load the URL contents into
6 div = $("<div>");
7
8 // When the element is displayed at first we could leave it blank
9 // 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.
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
// 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);
// By default we're going to hide the DIV until the content is loaded
div.hide();
// Before the DIV gets its content we need to put the DOM on the page
// and in to the body. $.prepend will put our content at the top of
// of the <body>. But this will be hid.
$("body").prepend(div);
// Now we load the content into the DOM and as a CALLBACK function
// we SLIDEDOWN the div. This could be FADEIN or something else.
div.load(u, {limit: 25}, function(){
div.slideDown();
});
1// Your URL of the page to be loaded
2 u = "/index.cfm?event=contactme";
3
4 // We're creating a div element to load the URL contents into
5 div = $("<div>");
6
7 // We now load our file and inject it into the DOM.
8 div.load(u);
9
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:
ColdFISH is developed by Jason Delmore. Source code and license information available at coldfish.riaforge.org
u = "/index.cfm?event=hermes.contactSeller";
div = $("<div>").html("Loading......").load(u);
$("body").prepend(div);
1u = "/index.cfm?event=hermes.contactSeller";
2 div = $("<div>").html("Loading......").load(u);
3 $("body").prepend(div);