Loading Please Wait...

Logo Lynxsia IT Solutions

jQuery Syntax

JQ Syntax

With jQuery you select (query) HTML elements and perform "actions" on them.

jQuery Syntax

The jQuery syntax is tailor-made for selecting HTML elements and performing some action on the element(s).

Basic syntax is: $(selector).action()

  • A $ sign to define/access jQuery
  • A (selector) to "query (or find)" HTML elements
  • A jQuery action() to be performed on the element(s)
					 
        
          $(this).hide()    // Hides current element
          $("p").hide()     // Hides all <p> elements
          $(".test").hide() // Hides all elements with class="test"
          $("#test").hide() // Hides all elements with id="test"
        
      
Similarity To CSS Selectors

jQuery uses CSS syntax to select elements. You will learn more about the selector syntax in the next chapter of this tutorial.

If you don't know CSS, you can read our CSS Tutorial.

The Document Ready Event

You might have noticed that all jQuery methods in our examples, are inside a document ready event:

					 
        
          $(document).ready(function(){
            // jQuery methods go here...
          });
        
      

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:

  • Trying to hide an element that is not created yet
  • Trying to get the size of an image that is not loaded yet

The jQuery team has also created an even shorter method for the document ready event:

					 
        
          $(function(){
            // jQuery methods go here...
          });
        
      

Use the syntax you prefer. We think that the document ready event is easier to understand when reading the code.

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved