Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load part page with jQuery & AJAX

Tags:

jquery

ajax

I have a list of dynamic links on page A and a div where I wish to load content from another dynamically generated page B based on PHP variables.

<a href="loader.php?id=1">Link 1</a>
<a href="loader.php?id=2">Link 2</a>
<a href="loader.php?id=3">Link 3</a>

This script successfully loads the external content from the loader.php page using the jQuery script below into #ajaxContent div.

$(document).ready(function(){ 
    $("a").click(function(){
        $.ajax({
            url:$(this).attr("href"),
            success: function(response) {
                $("#ajaxContent").html(response);
            }
        });   
        return false;
    });
});

My question is how can I load content from a named div element on the loader.php page using a modification of the script above? The reason for this is to show the content called using Ajax in its correct context on the natural url eg: href="loader.php?id=2 which is proper non JavaScript enabled / SEO practice I believe.

Update: Nearly there Explosion Pills ! many thanks.

Working code is.

$(".switchMe li a").click(function(){
$.ajax({
url:$(this).attr("href"),
success: function(response) {
//  $("#ajaxContent").html(response);
$("#ajaxContent").html($(response).find("#imageInfo"));
}
});

return false;
});

Thanks Jimmy

like image 551
Jimmy G Avatar asked Sep 04 '13 16:09

Jimmy G


People also ask

How to load external HTML page into Div using jQuery?

Use the jQuery ajax $.load method to load external html page or html page into a div. Ajax $ .load () method is fetch the data or content, another page into a div, external HTML into div from the other pages or server.

How to load jQuery code after loading the page?

- GeeksforGeeks How to load jQuery code after loading the page? Method 1: Using the on () method with the load event: The on () method in jQuery is used to attach an event handler for any event to the selected elements. The window object is first selected using a selector and the on () method is used on this element.

How to load data from another page into a Div using Ajax?

Ajax $.load () method is fetch the data or content, another page into a div, external HTML into div from the other pages or server.

What is the use of jQuery load?

jQuery load() Method. The jQuery load() method loads data from the server and place the returned HTML into the selected element. This method provides a simple way to load data asynchronous from a web server.


2 Answers

You could use .load as in

$("#ajaxContent").load($(this).attr("href") + " #named-div");

Otherwise, parse the response yourself:

$("#ajaxContent").html($(response).find("#named-div"));
like image 114
Explosion Pills Avatar answered Oct 18 '22 09:10

Explosion Pills


Checkout jQuery .load() method.

like image 30
Michael Malinovskij Avatar answered Oct 18 '22 08:10

Michael Malinovskij