I'm trying to do some jquery ajax and I want to know the right way to get the specific content to load. In my case it is loading the entire HTML document within my given area when what I want is the content within my div with id of "main" to load.
Here is the code:
(function($){
// target the links
$link = $('nav a');
// on click
$link.on('click', function(event){
// create variables
$el = $(this);
var value = $el.attr("href");
event.preventDefault();
$.ajax({
url: value,
method: 'POST',
data: { href: value }
})
.done(function( html ) {
$( "#main" ).append( html );
});
});
})(jQuery);
And here is the HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>AJAX jQuery</title>
<meta name="description" content="">
<meta name="author" content="">
<!-- Mobile Specific Meta -->
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"/>
<!-- Stylesheets -->
</head>
<body>
<nav>
<a href="/ajax-test">Main</a>
<a href="external.html">Some link</a>
</nav>
<div id="main">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<script src="https://code.jquery.com/jquery-3.1.0.min.js" integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s=" crossorigin="anonymous"></script>
<script src="ajax.js" charset="utf-8">
</script>
</body>
</html>
I want the content in the external.html to load but instead it loads the whole document including etc..
Thanks for your help!