Jqury Syntax


In this Article I am going to cover the next step towards learning jQuery 1. Basic jQuery Example
2. jQuery Syntax


In continuation to my previous article about introduction to jQuery

Basic jQuery Example



The following example demonstrates the jQuery hide() method, hiding all <p> elements in an HTML document.

An Example To Showing Hide Method of jQuery.


<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
</script>
</head>

<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>
</html>



jQuery Syntax



The Document Ready Function

You might have noticed that all jQuery methods, in our examples, are inside a document.ready() function:


$(document).ready(function(){

// jQuery functions go here...

});


This is because we want to prevent any jQuery code to run before the document is finished its loading or in other words we can say that the jquery code will run only once document or the page is ready.

to give a clear idea why we not like that jquery Code should run before document load is for examples if we try to hide an element that does not exist or we try to get the size of any image which has not loaded yet will fail if functions run before the document is fully loaded.

jQuery Syntax



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

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

A dollar sign to define jQuery
A (selector) to "query (or find)" HTML elements
A jQuery action() to be performed on the element(s)

Examples:

$(this).hide() - hides current element

$("p").hide() - hides all paragraphs

$("p.test").hide() - hides all paragraphs with class="test"

$("#test").hide() - hides the element with id="test"

Note :

jQuery uses a combination of XPath and CSS selector syntax.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: