-3

I need to load a file in div on click of another div :

My code so far :

<div id="load_home">HOME </div>
<div id="content"></div>

<script>
 $(document).ready(function(){
   $("#load_home").click(function(){
   $("#content").load('code.html');
  });
 });
</script>

1 Answers1

0

You can use javascript inside the click function to get the HTML file content and attach to the content div like this

<div id="load_home">HOME </div>
<div id ="content"></div>

<script>
 $(document).ready(function(){
   $("#load_home").click(function(){
      document.getElementById('content').innerHTML = loadContent();
  });
 });

function loadContent(){
  var xmlhttp = new XMLHttpRequest();
  var pathToFile = 'code.html';
  xmlhttp.open("GET", pathToFile , false);
  xmlhttp.send();
  return xmlhttp.responseText;
}
</script>

Notice that pathToFile variable holds the actual path of the html file you want to render.

Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62