2

What I am trying to do is to store a list of hundreds of files in an array and use jquery load() function to load their content one after one by a loop.

I have very little knowledge of jquery and javascript, and this is what I managed to write, but it is not working.

var files=['index1c.html','index23c.html'];

for(var index = 0; index < files.length; index++){
    var file = files[index];

$('#result').load('file .desc');
}

I think that I have some problem in variable file given in load() , is it the right way? It is working fine if I directly write a single file name in place of file like below:

$('#result').load('index1c.html .desc');
6
  • 2
    .load() will overwrite the contents of #result every time a new file loads. Is this what you intend to happen? Commented Dec 25, 2012 at 8:22
  • @Blender: no i want content to be appended. Ya this is what load() do, I did not payed attention to this. Any solution? Commented Dec 25, 2012 at 8:25
  • 1
    "hundreds of files". Poor little server, poor little client, poor little internet. Commented Dec 25, 2012 at 8:26
  • @Engineer: If a selector is passed after the URL, jQuery uses those elements instead of the whole page. Commented Dec 25, 2012 at 8:27
  • @Beetroot-Beetroot: I am doing it all on my local machine Commented Dec 25, 2012 at 8:30

3 Answers 3

3

Change:

$('#result').load('file .desc');

To:

$('#result').load(file + ' .desc');

Note that load method removes the element's content before appending new content. You can create unique IDs like result1, result2 ... and code:

for(var index = 0; index < files.length; index++){
    var file = files[index];
    $('#result' + (index + 1) ).load(file + ' .desc');
}

Or you can use classes and eq method:

var $results = $('.results')
for(var index = 0; index < files.length; index++){
    var file = files[index];
    $results.eq(index).load(file + ' .desc');
}

Or if you want to append the data you can use $.get utility function instead of load method.

Sign up to request clarification or add additional context in comments.

6 Comments

any way by which I can prevent it to get overwrite and instead get it appended ?
@Abhi You can use $.get utility function.
A possible issue Abhi - do you need to guarantee that the files' contents are listed in the same order they were requested? With a straightforward loop-load, this is not guaranteed.
@undefined: I tried using $.get but it looks like I need to use some server side tech to get data in proper format. I tried this code $.get('index1c.html',function(data){alert("Data Loaded: " + data);}); but it is giving Data Loaded: [object XMLDocument] and when I parsed xml, it is showing null
@Abhi Actually both load and $.get need server to work, if you have your server running create a jQuery object from the data returned by server find the element and then append it, $(data).find('.desc').appendTo('#result')
|
2

You're using the literal string 'file .desc'.

It looks like you want file + '.desc'.

'a b' is the string a b whereas a + ' b' is the string with the contents of the variable a appended to the string ' b'.

Comments

2

Try using jQuery.ajaxSetup({async:false});

{async:false} will hold the execution of rest code. Once you get response of ajax code, rest of the code will be executed. After executing the ajax response reset to async to true jQuery.ajaxSetup({async:true});

So final code will be

var files=['index1c.html','index23c.html'];

for(var index = 0; index < files.length; index++){
    var file = files[index];
    jQuery.ajaxSetup({async:false});
    $('#result').load('file .desc');
    jQuery.ajaxSetup({async:true})
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.