jQuery - get()
jQuery .get() is used when you want to get data from a server without reloading the page. It's a part of jQuery's AJAX features, which let your webpage talk to a server in the background.
Think of it like this: you click a button, and your page secretly asks the server for some data—then shows that data right away.
Basic Syntax:
$.get(URL, callback);
-
URL: the location of the data (usually a file or API)
-
callback: a function that runs when the data is received
Example: Load a text file and show it in a div
Let’s say you have a file called info.txt, and you want to show its content on the page:
$("#loadBtn").click(function() {
$.get("info.txt", function(data) {
$("#content").html(data);
});
});
Here’s what this does:
-
When you click the button with ID loadBtn, it sends a GET request to info.txt.
-
Once the file is loaded, it puts the text inside the element with ID content.
What Kind of Data Can You Get?
-
Text files (.txt)
-
HTML snippets
-
JSON data (from APIs)
-
Any content the server can send back
Why Use .get()?
-
It’s simple and fast
-
It avoids page reloads
-
It helps create dynamic, responsive websites