With jQuery you select (query) HTML elements and perform "actions" on them.
The jQuery syntax is tailor-made for selecting HTML elements and performing some action on the element(s).
Examples:
$(this).hide() - hides the current element.
You might have noticed that all jQuery methods in our examples, are inside a document ready event.
Example:
$(document).ready(function(){
});
This is to prevent any jQuery code from running before the document is finished loading (is ready).
It is good practice to wait for the document to be fully loaded and ready before working with it. This also allows you to have your JavaScript code before the body of your document, in the head section.
Here are some examples of actions that can fail if methods are run before the document is fully loaded:
$("#myId").text("New text here"); // Select by ID
$(".myClass").html("Bold text"); // Select by class
$("p").hide();
$("#btn").click(function(){ alert("Button clicked!"); });
$("h1").css("color", "red");
$("#box").hide(); // Hides the element
$("#box").show(); // Shows the element
$("#box").toggle(); // Toggles visibility
$.get("data.txt", function(data){ $("#output").html(data); });