jQuery jQuery Tutorial jQuery Effects jQuery HTML jQuery Traversing jQuery AJAX jQuery Misc



jQuery Syntax

jQuery is a popular JavaScript library that simplifies the process of creating dynamic and interactive web pages. It provides a wide range of features and functions that can be used to manipulate HTML elements, handle events, and perform animations. One of the key features of jQuery is its syntax, which is designed to be simple and easy to use.

The basic syntax of jQuery involves selecting HTML elements and then performing actions on them. The syntax is based on CSS selectors, which are used to identify elements on a web page. Once an element has been selected, jQuery provides a range of methods that can be used to manipulate the element.

Here are some examples of jQuery syntax:

<script>
$(document).ready(function(){
    $("button").click(function(){
        $("p").hide();
    });
});
</script>

In this example, the jQuery code is enclosed in a script tag. The code uses the document.ready() function to ensure that the code is executed only after the document has finished loading. The code then selects all the button elements on the page using the $("button") selector. When a button is clicked, the code uses the $("p") selector to select all the paragraph elements on the page and then hides them using the .hide() method.

<script>
$(document).ready(function(){
    $("button").click(function(){
        $("p").css("color", "red");
    });
});
</script>

In this example, the jQuery code is similar to the previous example, but instead of hiding the paragraph elements, it changes their color to red using the .css() method.

jQuery syntax can also be used to handle events, such as mouse clicks and keyboard input. Here is an example:

<script>
$(document).ready(function(){
    $("input").keypress(function(){
        $("span").text("You pressed a key!");
    });
});
</script>

In this example, the jQuery code selects all the input elements on the page using the $("input") selector. When a key is pressed in any of the input elements, the code uses the .text() method to change the text of the span element to "You pressed a key!".

Overall, jQuery syntax is designed to be simple and easy to use, making it a popular choice for web developers who want to create dynamic and interactive web pages. By using jQuery syntax, developers can easily manipulate HTML elements, handle events, and perform animations, without having to write complex JavaScript code.

References

Activity