I have the following code:
HTML
<div id="body"></div>
JS
var site = { 'pageData' : [
{
'loadInTo' : '#aboutUs',
'url' : 'aboutUs.html',
'urlSection' : '.sectionInner'
},
{
'loadInTo' : '#whatWeDo',
'url' : 'whatWeDo.html',
'urlSection' : '.sectionInner'
},
{
'loadInTo' : '#ourValues',
'url' : 'ourValues.html',
'urlSection' : '.sectionInner'
},
{
'loadInTo' : '#ourExpertise',
'url' : 'ourExpertise.html',
'urlSection' : '.sectionInner'
}
]}
for(i=0; i < site.pageData.length; i++) {
var loader = site.pageData[i];
$('#body').append('<div id="'+ loader.loadInTo +'" class="section" />');
$(loader.loadInTo).load(loader.url + ' ' + loader.urlSection);
}
What I am doing is looping through the site variable and writing out some div's using jQuery's append method that have the id set in 'loadInTo', this works fine. After this is complete I want to use jQuery's load method to populate the divs with HTML from other pages. Is there a to make a callback after appending the div's? something like this:
$('#body').append('<div id="'+ loader.loadInTo +'" class="section" />', function(){
$(loader.loadInTo).load(loader.url + ' ' + loader.urlSection);
});
In jquery you could use $() just after your appending contents code. This way you can be sure that the content is loaded and the DOM is ready before performing any tasks on the appended content.
$() calls a function that either registers a DOM-ready callback (if a function is passed to it) or returns elements from the DOM (if a selector string or element is passed to it)
You can find more here
difference between $ and $() in jQuery
http://api.jquery.com/ready/
Why a callback? Is
$(...).append()
an asynchronous call? I think not, so why not just write another statement?If what you're after is avoiding code-replication, you may create the new division in a temporary variable:
Where the createDiv function may look like
jQuery doesn't support a callback for
.append
. Also, it's much more efficient to append the data at once, rather than calling.append
for each element. See the code below.Every element-to-append is added to a string. Once the string has finished, a marker is added, and the HTML is appended. Then, a poller is activated, checking whether the
marker
element exist in the DOM. If it exists, the poller is cleared, the marker is removed, and the code executes.Update:
.substr(1)
is used when the ID is set, because the ID shouldn't be prefixed by#
.