Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load an HTML fragment with JQuery ajax()?

Tags:

jquery

Please can someone tell me how to extract the content from a div on another page using the JQuery ajax() method?

anotherpage.html

<html>
<head></head>
<body> 
<div id="content">I want to extract this element</div>
</body>
</html>

I've tried:

$.ajax({
url: "anotherpage.html #content",
dataType: "html"
}).done(function(data){
$("#results").html(data);
})

But it returns the complete page and not just the #content fragment.

NOTE: I know how to get it working with the load() shorthand but I'd like to know how to do it with the ajax() method.

Thanks in advance.

like image 377
user1791841 Avatar asked Jun 02 '13 17:06

user1791841


1 Answers

try with this

 $('#results').load("anotherpage.html #content");

also visit "Loading Page Fragments" here jquery load api.

like image 91
Nikola Mitev Avatar answered Nov 10 '22 01:11

Nikola Mitev