Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have multiple load functions which I would like to have compressed into one line if possible. The #id's and links are "filler" content.

Here is my code...

$('#headlines-container').load('pages/headlines.html');
$('#standings-container').load('pages/standings.html');
$('#other-container').load('pages/other.html');

Is it possible to load all of the pages with one .load() while placing the content into their respective containers?

share|improve this question
1  
Is there a reason you need them on one line? – James Khoury Aug 15 '12 at 5:52
No particular reason, just wanted to know if it was possible. I am always looking for new ways to shorthand my code. – japanFour Aug 15 '12 at 14:27
1  
I wouldn't worry about it unless you have many of these. 3 lines really isn't a lot and you'd be adding complexity for no reason. – James Khoury Aug 15 '12 at 23:45

1 Answer

up vote 1 down vote accepted

I'd try something along these lines (not 100% sure it will work though):

$('#headlines-container, #standings-container, #other-container').each(function() {
    var e = $(this),
        id = e.attr('id');
    e.load('pages/' + id.split('-')[0] + '.html');
});
share|improve this answer
-1 It will not. You would at least need to deliver a function to load, which would calculate and return the string as you are doing now. But there is no such overload for load. – ANeves Aug 15 '12 at 8:35
that's a bit of a shame for jQuery in such case, I'd expect it to work... but nonetheless, I've updated this to use each to accomplish the same task - that approach should work now – Zathrus Writer Aug 15 '12 at 10:02
thx for corrections ANeves – Zathrus Writer Aug 15 '12 at 17:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.