0

I have this code in header.html:

<nav role="navigation">
    <ul class="nav">
        <li><a id="home" href="index.html" class="current">home</a></li>
        <li><a id="about" href="includes/test.html">about</a></li>
        <li><a id="resources" href="#">resources</a></li>
        <li><a id="archive" href="#">archives</a></li>
    </ul>
 </nav>

Now, everything seems fine until my index.html loads files from another folder to fill the divs with id="header", id="content" and id="footer" using:

$(function() {
    $("#mainhead").load("templates/header.html");
    $("#content").load("templates/content.html");
    $("#footer").load("templates/footer.html");
});

My question is - How can I replace content in the div id="content" with the one from another HTML page that can be accessed by clicking a link in the nav menu?

4
  • Do you have to contents that you want to put into the div as a string? Commented Sep 9, 2015 at 12:19
  • Is $('nav a').click(function() { $('#content').load(this.href) }); what you mean? Commented Sep 9, 2015 at 12:22
  • 1
    my guess is you would put load calls similar to this in a click handler, and attach that to the click event of the menu item? Commented Sep 9, 2015 at 12:22
  • Load the content onclick and not on document ready. Commented Sep 9, 2015 at 12:23

4 Answers 4

3

Change the code that loads the navigation to this...

$("#mainhead").load("templates/header.html", function() {
    $(this).find(".nav a").on("click", function(e) {
        e.preventDefault();             // stop the link from firing as normal
        $("#content").load(this.href);  // load the file specified by the link
    });
});

This will "ajaxify" the links in the header navigation, after it has loaded, so that the content div is loaded for each of the links, without having to navigate away from the page. Just make sure each header link has the correct href value for the content you want it to load.

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

2 Comments

Thank you very much, worked like charm, it's been two days since I was looking for a solution, but nothing seemed to work, thank you very much again
No problem - glad I could help :)
0

Is this what you meant? This will load the content from header/content/footer pages and load in the appropriate div on click of home.

$("#home").click(function () {
    $("#mainhead").load("templates/header.html", function (data) { $(this).html(data); });
    $("#content").load("templates/content.html", function (data) { $(this).html(data); });
    $("#footer").load("templates/footer.html", function (data) { $(this).html(data); });
});

1 Comment

already done that, the problem was when i wanted to change to another page (like "about", "resources", etc) to not redirect me to that page, but to replace the actual content in the #content div with the one from the "about" page.
0

$("#content").replace("old page","new page");

try this one.

or

$("#content").load(){ location.replace("your page"); }

or

var url = "Your url"; $(location).attr('href',url);

1 Comment

tried this one too, didn't help much either, thank your for the answer though :D
-2
$("#div").html("Your content");

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.