0

I'm having trouble loading an HTML file into a div using Jquery. From what I know, this should work, and the html file i'm loading has a simple tag in it saying "Hello World!" However, when I click the link that should trigger the event and load the HTML into the div, nothing happens, and "Hellow World!" doesn't display anywhere on the screen. Here's the code:

<html>
<link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="bootstrap.css">
  <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css"> <script src="js/bootstrap.js"></script>
  <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<head>
</head>
<script src="js/funstuff.js"></script>
<body background="images/home_cooking.jpg" height="1000px">
     <div class="navbar navbar-default navbar-fixed-top" role="Navigation" style="margin-bottom: 0">
     <div class="container">
        <div class="navbar-header">
          <button class="navbar-toggle" type="button" data-toggle="collapse" data-target="#navbar-main">
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
         </button>
         <a href="#" class="navbar-brand" style="font-size: 29px; margin-top: 7px">Cookbook</a>
        </div>
          <div class="navbar-collapse collapse navbar-responsive-collapse">
            <ul class="nav navbar-nav">
              <li> <a href="#" style="padding-top: 24px; padding-bottom: 28px; font-size: large; font-weight: 600; text-shadow: 0 0 5px #000000">About us</a> <li>
              <li> <a href="#" id="load_home" style="padding-top: 24px; padding-bottom: 28px; font-size: large; font-weight: 600; text-shadow: 0 0 5px #000000">Contact us</a></li>
            </ul>
            <form action="userlogin.html" class="navbar-form navbar-right" method="post">

              <div class="form-group">
                <input type="text" class="form-control col-lg-8" placeholder="Username" name="user">
                <br> <input type="checkbox" name="check"> Remember me
              </div>
              <div class="form-group">
                <input type="password" class="form-control col-lg-8" placeholder="Password" name="pass"> <br>
                <a href="#">Forgot your password? </a>
              </div>
              <button type="submit" class="btn btn-primary" style="margin-bottom: 20px">Login</button>

            </form>
          </div> 
        </div>
      </div>
    </div>
  <div id="content">

  </div>
  <script>
$(document).ready( function() {
    $("#load_home").on("click", function() {
        $("#content").load("test.html");
    });
});
</script>
</body>
</html>

I apologize if the indentation an everything is sloppy, i'm just trying to get something functional right now! Any help would be greatly appreciated!

EDIT: It's also worth noting that everything is based locally on my computer. test.html is located in the same folder as index.html.

2 Answers2

0

You need to return false to cancel the default action of the anchor tag

 $(document).ready( function() {
    $("#load_home").on("click", function() {
        $("#content").load("test.html");
        return false;
    });
});
pizzarob
  • 11,711
  • 6
  • 48
  • 69
0

You can also write the script in this way:

$(document).ready( function() {
   $("a").on("click", function() {
     var id=$(this).attr('id');
     if(id=="load_home"){
       $("#content").load('test.html');
     }
   });
});
devX
  • 65
  • 11