0

I am using this code to load some contents of a file into a div:

$('#mydiv').load('template1.html');

Is it possible to load multiple into multiple div's?

For example:

$('#mydiv', '#mydiv2').load('template1.html', 'template2.html');

If so how?

8
  • 2
    No, you can't. Period. Commented Dec 21, 2015 at 11:15
  • No ways around it then? Commented Dec 21, 2015 at 11:16
  • You can write a function which will do this. Natively not possible. Commented Dec 21, 2015 at 11:20
  • @Vohuman You can't using the $.load() function, coz it supports only one URL. Commented Dec 21, 2015 at 11:21
  • 1
    @Vohuman Read well: Javascript / jQuery .load loading multiple files into div's, and not using any other way. Commented Dec 21, 2015 at 11:25

2 Answers 2

2

According to the documentation, no. You can't.

But if you would like to automate it, you can do using an associative object may be? This is just a try:

ajaxLoads = {
    "#div1": "template1.html",
    "#div2": "template2.html",
    "#div3": "template3.html",
    "#div4": "template4.html",
    "#div5": "template5.html"
}
$.each(ajaxLoads, function (div, url) {
    $(div).load(url);
});
Sign up to request clarification or add additional context in comments.

Comments

0

Another way using jQuery:

let pages = [
    'page1.html', 
    'page2.html'
];

$.each(pages, function(_key, value) {
    $.get(value, {}, function(result) { 
        let $html = $('<div />').html(result); 
        $('#mydiv').append($html); 
    },'html');
});

1 Comment

Note: in this case, multiple html files are appended to the same div.

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.