Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the html of a div on another page with jQuery ajax?

I'm using jQuery's ajax code to load new pages, but wanted him to get only the html of a div.

My codes: HTML:

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

Script:

$.ajax({    url:href,    type:'GET',    success: function(data){        $('#content').html(data);    } }); 

I wanted him to get only the html of $('div#content') on another page. How to do it?

like image 416
Iago Bruno Avatar asked Sep 21 '13 22:09

Iago Bruno


People also ask

How do I get HTML in AJAX?

$. ajax({ url:href, type:'GET', success: function(data){ $('#content'). html(data); } });

How do I go to HTML from another page?

To replace the entire contents of a page with content from another, you would do this: getHTML( '/about', function (response) { document. documentElement. innerHTML = response.

How AJAX get data from another page in php?

php", success: function(data){ $. ajax({ url: "data. php? id=data" } });


2 Answers

Ok, You should "construct" the html and find the .content div.

like this:

$.ajax({    url:href,    type:'GET',    success: function(data){        $('#content').html($(data).find('#content').html());    } }); 

Simple!

like image 127
Gustavo Costa De Oliveira Avatar answered Sep 21 '22 20:09

Gustavo Costa De Oliveira


You can use JQuery .load() method:

http://api.jquery.com/load/

 $( "#content" ).load( "ajax/test.html div#content" ); 
like image 42
nilgun Avatar answered Sep 25 '22 20:09

nilgun