2

I want to load the contents of an html file (home.html) into a div container located inside of a main html file (index.html)after clicking on a link and I want to use jquery/ajax to do it. I have tried to find answers but have only come across answers pertaining to php and not html.

this is the html code I have so far:

  <!-- navigation bar -->
  <nav>
    <ul>
      <li><a href="#" data-target="home.html">HOME</a></li>
      <li><a href="#" data-target="work.html">MY WORK</a></li>
      <li><a href="#" data-target="about.html">ABOUT ME</a></li>
      <li><a href="#" data-target="services.html">SERVICES</a></li>
      <li><a href="#" data-target="contact.html">CONTACT ME</a></li>
    </ul>
  </nav>

  <!-- content div -->

  <div id="content">

  </div>

  <!-- footer -->
  <section>
  </section>
  <footer>
    <p>Copyright &copy; 2017, All Rights Reserved</p>
  </footer>
</div>
7

2 Answers 2

2

You can try the following; here you are getting the contents of load.html and putting it inside the content div.

    <!-- navigation bar -->
          <nav>
            <ul>
              <li><a href="#" data-target="home.html">HOME</a></li>
              <li><a href="#" data-target="work.html">MY WORK</a></li>
              <li><a href="#" data-target="about.html">ABOUT ME</a></li>
              <li><a href="#" data-target="services.html">SERVICES</a></li>
              <li><a href="#" data-target="contact.html">CONTACT ME</a></li>
            </ul>
          </nav>

          <!-- content div -->

          <div id="content">

          </div>

          <!-- footer -->
          <section>
          </section>
          <footer>
            <p>Copyright &copy; 2017, All Rights Reserved</p>
          </footer>
        </div>

<script>
$( "#content" ).load( "/resources/load.html" );
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

The jQuery code could look like this:

$('a').each(function(b,c){     //loop through each a element
  $(c).on('click',function(){   //add click event listener
      var link = $(this).attr('data-target');  //get the link from attribute
      $('#content').load(link);    //load the link's content into container
  });
});

It will loop through all a elements and get the link from data-target attributes. Just remember to add jQuery to your webpage.

Comments

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.