jQuery .load() requests HTML from a URL and places the returned markup inside the matched element. It is convenient for partial-page updates when the backend returns HTML rather than JSON.
$( "#result" ).load(
"content.php"
);
$( "#b1" ).on( "click", function () {
$( "#result" ).load(
"content.php"
);
});
See click events.
const id =
$( "#record_id" ).val();
$( "#result" ).load(
"record.php?id=" +
encodeURIComponent( id )
);
Prefer a data object when practical:
$( "#result" ).load(
"record.php",
{
id: $( "#record_id" ).val()
}
);
When a data object is supplied, jQuery .load() uses POST.
$( "#result" ).load(
"content.php",
function (
responseText,
textStatus,
jqXHR
) {
if ( textStatus === "error" ) {
$( this ).text(
"Unable to load content."
);
}
}
);
If loaded content contains new buttons or links, bind delegated events to a stable ancestor instead of expecting a handler attached before loading to find future elements.
$( "#result" ).on(
"click",
".edit-button",
function () {
console.log(
"Clicked record:",
$( this ).data( "id" )
);
}
);
Demo: delayed message loaded into a page →
<button type="button" class="btn btn-info"
onclick=$("#d").load("load-text.php");>Load Page</button>
<div id='d'></div><i>This message is from load-text.php file</i><button type="button" class="btn btn-info" id=b1>Load Page</button>
<div id='d1' ></div><script>
$(document).ready(function() {
$("#b1").click(function(){
$("#d1").load("load-text1.php?msg=Hello_world");
})
})
</script>var msg=encodeURIComponent("Hello World");
$("#d1").load("load-text1.php?msg="+ msg);<?Php
$msg=$_GET['msg'];
echo "This is from load-text1.php file with Message : $msg ";
?>First Name<br><input type=text name=t1 id=t1>
Last Name<br><input type=text name=t2 id=t2>
<br><button type="button" class="btn btn-info" id=b2>Load Page</button>
<div id='d2' ></div><script>
$(document).ready(function() {
$("#b2").click(function(){
str1=$('#t1').val();
str2=$('#t2').val();
$("#d2").load("load-text2.php", {'t1':str1,'t2':str2});
})
})
</script><?Php
$t1=$_POST['t1'];
$t2=$_POST['t2'];
echo "Welcome $t1 $t2";
?><button type="button" class="btn btn-info" id=b3>Load Page</button>
<div id='d3' ></div><script>
$(document).ready(function() {
$("#b3").click(function(){
$("#d3").load("load-text.php",function(){
alert ( ' Content of load-text.php is loaded');
})
})
})
</script><script>
$(document).ready(function() {
/////
$("#b1").click(function(){
$('#msg').html('Wait .. ');
$('#msg').show();
$("#d1").load("load-delay-msgck.php", function() {
$('#msg').html('Done .. ');
setTimeout(function() { $("#msg").fadeOut('slow'); }, 4000);
});
})
////////////////
})
</script>my_load_function=function my_function(){
$('#display_body').load("category-display.php");;
}
///calling the function ///
my_load_function();$('#display_body').on('click', ".edit_class", function () {
// Your code here
})
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
21-04-2020 | |
| Thank you so much. This solution really worked for me , i have been struggling to to post table row id to external php page by row click in jquery | |
02-06-2021 | |
| Thanks for the post. It has been very useful for me. | |