Syntax of jQuery

Syntax of jQuery
As we know now that jQuery is mainly used to select an HTML element and perform some action on that element.
So the general syntax of jQuery is –
$(selector).action()
Where -  
$-  is alias of jquery or a sign to define jQuery.
Selector – is an element selector.
Action – is action performed on that element.
Example –
$(“p”).hide()  - Hide all ‘p’ elements in the HTML page.

We already know that jQuery code is written in <script> tag in head section of HTML page. So now we will write our first jQuery program which will make you understand jQuery syntax.

Let’s create a file called “Index.html”
In this example we have used alert() function which will print “Welcome to jQuery tutorial.” When HTML DOM is full loaded or ready to execute jQuery code.
In above example you would have noticed that we wrote alert function inside $(document).ready() event. So now we will understand the use of this.

The Document ready Event
$(document).ready(function(){
    // jQuery methods goes here…
});
Almost in every jQuery program we will use the above syntax. Now what is this? And Why we use this?
$(document).ready()  -   is an event which fires when our DOM is fully loaded. Means in above syntax our jQuery code will execute when HTML DoM is fully loaded.
  • This prevents any jQuery code running before the document finish loading. It is always a good practice to wait for the document to be fully loaded.
  • This also allow you to write our jQuery code in head section of HTML page.
Here is another example which will make you understand the jQuery syntax.

Let’s save this code in a file called “another.html”
This will alert a message “Button clicked.” When button is clicked.
Hope You understand the basic syntax of jQuery. If still you have some problems in this. Don't worry We will use this syntax in almost all the jQuery program. So will grasp it.


Previous Page                                                                                      Next Page


Comments

Popular posts from this blog

Welcome Post