jQuery Syntax



jQuery Syntax : The Basic Idea about jQuery is to select the element(s) and perform the action on them. For selecting elements the selectors are used.


jQuery Syntax

:

The $ symbol is used to access the jQuery.
The basic example for jquery selector and action :

$(selector).action();

selector is used to select the element from the dom.
.action() performs the action on the selected element.

Example

jQuery Basic Example :

<!DOCTYPE html>
<html>
<head>
<title>jQuery CDN Example</title>
 <script type="text/javascript" src="//code.jquery.com/jquery-2.1.4.min.js">
 </script>
<script type="text/javascript">
         $(document).ready(function(){
          $(".test").css("background","blue");
          });
</script> 
  
</head>	
<body>
<div class="test"> 
This id test div .....and background is added using jquery...
</div>
</body>
</html>  

Try it »

Output

jquery basic syntax

jquery basic syntax

jQuery $(document).ready method

jQuery document ready event is used to prevent code run while your document ie. page is loading. It runs the code only when your document is completely loaded and ready.
This is used because some elements ie. selectors are created later in that case your code will not work for those
elements.

Best Example can suppose your code run first in head section and your element is loaded in footer later, in this case your code will not work.

example of document ready method :

   $(document).ready(function(){
          // write your code here...
   });

or you can also use

  $(function(){
          // write your code here...
   });

Advertisements

Add Comment

📖 Read More