0

So this first jQuery works. It loads a specific section of another html page into an id of #main_text.

$(function() {
  $('[id^="nav_"]').on("click", function() {
    var letter = this.id.split('_')[1];
    $("#main_text").load("page" + letter + ".html #main_text p,h2,h3,span #hidden");
  });
});

I now want to load multiple html files into that #main_text. So I tried to push them into an array and then .load that array. It's not working. Is this even possible?

$(document).ready(function() {
    var array = [];
    array.push('page_mark1.html');
    array.push('page_mark2.html');
      $('#mark').on("click", function() {

        $("#main_text").load('array');
      });
});
2
  • 1
    No Chris, what you are trying to do is kind of confused. You simply cannot load html pages that way. Commented May 10, 2016 at 18:50
  • Darn. I'm trying to pull div content from multiple pages into one page. Is there any way to do that? I can pull div content from one page into another just not multiple pages. Commented May 10, 2016 at 18:54

1 Answer 1

1

load method doesn't accept an array of urls. Also note that if you want pass the array you should pass array an not "array" (which is a string) to the function.

You can use the $.get function instead of the load method:

// create an array of deferred objects ( array of Ajax deferreds )
function getDef(urls) {
   return urls.map(function(url) { return $.get(url) });
}

// Pass the array to $.when
// the callback is called when all the requests are complete
$.when.apply($, getDef(array))
 .then(function() {
      // concatenate the strings based on the unknown length of arguments
      // `[].slice.call(arguments)` converts the special 
      // `arguments` object into a regular array
      var allContents = [].slice.call(arguments).join('');
      $("#main_text").html(allContents);
  });

You can also define a jQuery function:

$.fn.loadMany = function(urls) {
    var $this = this;
    urls = urls.map(function(url) { return $.get(url) });
    $.when.apply($, urls)
     .then(function() {
         $this.html([].slice.call(arguments));
     });
    return $this;
}

$("#main_text").loadMany(array);
Sign up to request clarification or add additional context in comments.

7 Comments

I think I'm in over my head. I can't get either of these to work. I don't see where the onclick event is either.
@Chris There is no click handler in the code. Put the jQuery function definition ($.fn.loadMany = function(urls) {... lines) before the $() call and replace the load call with loadAll.
I'm going to have to muddle through with this for a while to piece it together. Man, you read one jquery book and think you can do this stuff.
@Chris Sorry if the code is confusing. I know that I sometimes create quasi-cryptic scripts. Maybe that's because I'm a freelancer.
Thanks for commenting your code. Big help as I'm learning. I'm still unclear where to add the click handler. :(
|

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.