In my website I have a file called learn-more.html , it's used to load stuff from index.html using AJAX.
learn-more.html
<div id="something" class="hero-unit span-one-third" style="position: relative;">
Foo Bar
</div>
<div id="main-images" class="hero-unit span-one-third" style="position: relative;">
<div id="learn-more-photo" class="span-one-third">
<img class="thumbnail" src="http://placehold.it/300x180" alt="">
</div>
</div>
I have the following script located in index.html
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$("#learn-more-button").click(function() {
$.get('ajax/learn-more.html #main-images', function (data) {
alert(data);
});
</script>
Problem:
The alert prints the contents of: learn-more.html
<div id="something" class="hero-unit span-one-third" style="position: relative;">
Foo Bar
</div>
<div id="main-images" class="hero-unit span-one-third" style="position: relative;">
<div id="learn-more-photo" class="span-one-third">
<img class="thumbnail" src="http://placehold.it/300x180" alt="">
</div>
</div>
Expected
The alert should print the content of: <div id="main-images">
<div id="learn-more-photo" class="span-one-third">
<img class="thumbnail" src="http://placehold.it/300x180" alt="">
</div>
What could be going on here ?
EDIT: I'm willing to upload a .zip with the real files... it's just index.html plust learn-more.html and the css, if anyone is interested let me know.
EDIT2: This prints out the whole learn-more.html content
$.get('ajax/learn-more.html #main-images', function (data) {
alert(data);
});
var foo = $(data).html()? You're putting the results of the ajax call into a DOM fragment and then extracting the HTML from that. Why not justalert(data)?$.loadinstead of$.get$.load()is a little bit different than$.get();, so you can't just switch the words in your code. With$.get();you'd need to be using server-side scripting on the page you're calling (php or asp or etc. instead of html). With$.load()you can get specific elements using the hashtag identifier for theidattribute as you've written it in your code.