jQuery syntax is predefined and can be used to select and manipulate HTML elements.
jQuery Syntax
Syntax:
$(selector).action();
- The $ sign is used to access the jQuery function.
- The $ sign is an alias to jQuery()
- A (selector) is used to find and select the element from the DOM.
- jQuery uses CSS syntax to select the element(s).
- action() performs the action on the selected element(s).
You can use jQuery instead of a $ symbol because the $ symbol is a synonym of jQuery.
jQuery Example with HTML
Example:
<!DOCTYPE html>
<html>
<head>
<title>jQuery with HTML Example</title>
<script src = "//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<script>
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
});
});
</script>
<p>This is sample text.</p>
</body>
</html>
In the above example, the paragraph will disappear on click.