2

I'm using jquery to load my website content once its fully loaded. That works great.

jquery code

function check() {
$("#content").load('items.html');
}
$(document).ready(function() {
check(); 
});

html

<div id="content">

</div>

Is there a way without refreshing the whole page to dynamically load html element (div) when user clicks on one of the items which were already loaded('items.html')?. On one click I want to remove already loaded 'items.html' inside #content div and load new div from any.html file.

So basically, I need to show more info for that particular item by dynamically replacing already loaded items.html and adding new div instead.

I managed to load new html page by adding anchor tags on every item, but it would be much better if I could load only one part(div) of a html file and not a whole page, that way I can write all items informations in only one html file, and then add those div's dynamically when user clicks on any item.

2 Answers 2

2
function check() {
  $("#content").load('items.html #loadable'); // #ID to load
}

$('#loadCont').click(check); 

main page example
page 2 example

You might also want to put this somewhere

$.ajaxSetup({ cache: false });

https://api.jquery.com/jquery.ajaxsetup/

As seen from the demo, .load() does already content-replacement, so no need to .empty() or .html('') beforehand.

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

Comments

0

Simple.Try to call the check() function on clicking of the element

$('element').click(function(){
    check();
});

3 Comments

Thank you. I get that, but how can I target specific .html, and then specific div inside that html file with this jquery function? I hope it makes any sense what I'm asking.
@maddesign this is explain in DOC, read it and be aware, GET requests are cached api.jquery.com/load/#loading-page-fragments
That's why I added also

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.