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
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.
- 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.
Ajax $.load () method is fetch the data or content, another page into a div, external HTML into div from the other pages or server.
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.
You could use .load
as in
$("#ajaxContent").load($(this).attr("href") + " #named-div");
Otherwise, parse the response
yourself:
$("#ajaxContent").html($(response).find("#named-div"));
Checkout jQuery
.load() method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With