jQuery Syntax
jQuery Syntax
There are different ways to include jQuery in our project. One method is very simple and it is by using the CDN link in our project. The other is by downloading the file and then using it in the project. We can use the jQuery link anywhere whether in the head or body. Just make sure that it should be above the jQuery code that you will write. In our topics, we will use the CDN link of jQuery. The best practice for writing jQuery code is:-
Example Code:-
$(document).ready(function() {
$(function() {
// jQuery code goes here
});
});
Example Explanation:-
In the above code, the ready function will make sure that jQuery will only run when the page is fully loaded. Similarly, $ is used to access jQuery. From here, the code accesses the document object and defines a function to be called when the document's ready event is fired. The basic purpose of jQuery is the same as that of JavaScript. It is used to select an element present in the document to perform some actions on it.
Syntax:-
$("selector").action()
Syntax Explanation:-
The $ accesses jQuery.
The (selector) finds HTML elements.
The action() is then performed on the element(s).