2

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).

2 Answers 2

2

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).

Sign up to request clarification or add additional context in comments.

Comments

2

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"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.