I want to achieve something like this. Here the page automatically loads new content when I scroll down. I want to fetch the new set of data by clicking on a hyperlink at the bottom of my page.
Being a newbie to AJAX, I was checking out this question which is similar to mine, but it isn't working. All I get is an empty Object when I run the code. Here's the code in my files:
index.php
<a href="about.php">About Me</a> <br>
<a href="contact.php">Contact Me</a>
<div class="wrap"></div>
<script>
(function(){
var wrap = $('div.wrap');
$('a').on('click', function(e){
var href = $(this).attr('href');
$.get(href, function(data){
console.log($(data).find('div.container'));
$(data).find("div.container").appendTo(wrap);
});
e.preventDefault();
});
})();
</script>
about.php
</div>
<div class="container">
<h2>About Me</h2>
<p>I suck at AJAX ! :-(</p>
</div>
contact.php
<div class="container"><h1>Contact!</h1>
<form action="#">
<textarea name="content" id="content" cols="30" rows="10"></textarea>
<input type="url" name="url" id="url">
<p><button type="submit">Save</button></p>
</form>
</div>
Screenshot:
Am I missing something? Does .get()
work inside the callback function of click()
event? But it works fine when I .load()
it... Sorry for the big post but I'm totally at a loss here! :/ Please help me out?