pardeep pardeep - 1 year ago 93
Ajax Question

jQuery's ajax code to load new pages, but I want to get only the html of a div

HTML Code :

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


Script:

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


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

Answer Source

Here you go with a solution

At a single point of time, single page will rendering into the browser. So, you can't find the previous page HTML content using JavaScript/ jQuery into another page.

For that you need to use localStorage.

Page 1 HTML

<div id="contentToCopy">
  <div>
    Test
  </div>
</div>

Page 1 JavaScript/jQuery

localStorage.setItem('copiedContent', $('#contentToCopy').html());

Page 2 HTML

<div id="contentToPaste"></div>

Page 2 JavaScript/jQuery

$('#contentToPaste').html(localStorage.getItem('copiedContent');

Hope this will help you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download