Thomas
Thomas

Reputation: 5089

Filter strings with jQuery/Javascript

Im trying to use $.get to load pages without reloading the page.

<script>
$('#top_links a').click(function (e) {
    e.preventDefault();
    var link = $(this).attr('href');
    $('#content').empty();


    $.get(link, { }, 
        function(data) {
            $('#content').empty().append(data);
        }
    )
});
</script>

This works but the entire requested page is getting stuffed into #content. Instead of detecting the ajax request and filtering the data on the server-side, I would like to do it client-side. Is there a way to use javascript to effectively filter the string so that I only get certain divs? The content that I want to preserve will all be contained inside of a div called #content ( Im just trying to swap the current page's #content with the requested page's #content).

Upvotes: 2

Views: 124

Answers (2)

James Montagne
James Montagne

Reputation: 78650

You can manipulate the data returned from get in the same way you do the elements on the page.

For instance:

$('#content').replaceWith($(data).find("#content"));

Upvotes: 2

VisioN
VisioN

Reputation: 145398

Try load method with loading of page fragments approach:

$("#content").load(link + " #content > *");​​​​​​​​​​​

Here we can use > * in order to include the inner HTML of the #content block only without surrounding it with <div id="content"></div> (check more information about it here).

Upvotes: 2

Related Questions