Wednesday 20 May 2015

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()
Example:
(this).hide() - hides the current element.
$("p").hide() - hides all <p> elements.
$(".test").hide() - hides all elements with class="test".
$("#test").hide() - hides the element with id="test".
The Element Selector:
For Button->
When a user clicks on a button, all <p> elements will be hidden:
$(document).ready(function(){
    $("button").click(function(){
        $("p").hide();
    });
});
The #id Selector:
When a user clicks on a button, the element with id="test" will be hidden:
$(document).ready(function(){
    $("button").click(function(){
        $("#test").hide();
    });
});

The .class Selector:
When a user clicks on a button, the elements with class="test" will be hidden:
$(document).ready(function(){
    $("button").click(function(){
        $(".test").hide();
    });
});

Same above functions  can be in separate file -
If your website contains a lot of pages, and you want your jQuery functions to be easy to maintain, you can put your jQuery functions in a separate .js file

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js">
</script>
<script src="my_jquery_functions.js"></script>
</head


No comments:

Post a Comment