jQuery is tailor-made to respond to events in an HTML page.
All the different visitor's actions that a web page can respond to are called events.
An event represents the precise moment when something happens.
Examples:
$(this).hide() - hides the current element.
$("p").hide() - hides all <p> elements.
$(".test").hide() - hides all elements with class="test".
$("#test").hide() - hides the element with id="test".
jQuery Selector allow you to select and manipulate HTML elements.
jQuery Selector are used to find or select HTML elements based on their name,id,class,types,attributes
The ready method specifies what happens when a ready event occurs
You might have noticed that all jQuery methods in our examples, are inside a document ready event:
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:
The click() method attaches an event handler function to an HTML element.
The function is executed when the user clicks on the HTML element.
The following example says: When a click event fires on a <p> element; hide the current <p> element:
The dblclick() method attaches an event handler function to an HTML element.
The function is executed when the user double-clicks on the HTML element:
The focus() method attaches an event handler function to an HTML form field.
The function is executed when the form field gets focus: