Before learning jQuery, you should learn HTML, CSS, JavaScript, and learn how to debug JavaScript. You should also be familiar with JSON. Knowing XML will also be helpful.
jQuery is a free and open source JavaScript library. It simplifies common web tasks such as state initialization or updating elements and makes it easy to make AJAX calls, manipulate content, and create animations. It also does the dirty work of dealing with cross-browser compatibility issues. jQuery’s syntax is much simpler and concise than the syntax of vanilla JavaScript.
JQuery uses CSS syntax for many operations. It works easily with elements and sets of elements. It can also be extended with plugins. You use free plugins or create your own.
First example
- Add source of jQuery script
- Write jQuery code in head
- Add div where jQuery output will be inserted
Selectors
Selectors and filters are used to extract information from web pages. To select an element, use
$("p")
This selects all <p> html elements. The following code will select all HTML elements with the class classes with the name “myclass”:
$(".myclass")
The following code will select all HTML elements with the identifier “myid”:
$("#myid")
Following example shows how the three types of selector are used.
<html>
<head>
<title>Selectors</title>
<script type="text/javascript" src="jquery-3.1.1.js"></script>
<script type="text/javascript">
$("document").ready(function() {
$("h1").css("border", "5px solid red");
$(".myclass").css("border", "5px solid green");
$("#myid").css("border", "5px solid blue");
});
</script>
</head>
<body>
<h1>Tag selector</h1>
<h2 class="myclass">Class Selector</h2>
<h3 id="myid">Id Selector</h3>
</body>
</html>
Filters
Filters are used to narrow down results of selectors. :first selects the first instance of the selector. :last selects the last instance of the selector. p:not(p:eq(2)) means select all <p> tags except the one with index=2. Since indexing starts with 0, this will select all except the third. :even select even number indexed tags and :odd is for odd numbered. Since the indexing starts with 0, even would select first, third, fifth, etc. See the code below:
<html>
<head>
<title>Filters</title>
<script type="text/javascript" src="jquery-3.1.1.js"></script>
<script type="text/javascript">
$("document").ready(function() {
$("#one p:first").css("border", "5px solid red");
$("#one p:last").css("border", "5px solid red");
$("#two p:even").css("border", "5px solid green");
$("#two p:odd").css("border", "5px solid blue");
$("#three p:not(p:eq(2))").css("border", "5px solid orange");
});
</script>
</head>
<body>
<h3>:first and :last</h3>
<div id="one">
<p>First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>
</div>
<h3>:even and :odd</h3>
<div id="two">
<p>First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>
<p>Fourth paragraph</p>
</div>
<h3>All except one</h3>
<div id="three">
<p>First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>
<p>Fourth paragraph</p>
</div>
</body>
</html>
Editing Page Content
To edit page content, you use html(), prepend() or text() functions. html() will replace existing content. prepend() will add content in front of existing content. text() will add new content. Following code shows how they are used:
<html>
<head>
<title>Creating and Changing Content</title>
<script type="text/javascript" src="../jquery-3.1.1.js"></script>
<script type="text/javascript">
$("document").ready(function () {
var newcontent = $("<p>");
newcontent.append("Added this text");
$("#txt1").html(newcontent);
$("#txt2").html("Replaced existing content")
$("#txt3").prepend("Added this text before ");
$("#txt4").text("Adding new text");
});
</script>
</head>
<body>
<div id="txt1"></div>
<div id="txt2">This will be replaced</div>
<div id="txt3">existing text</div>
<div id="txt4">new text</div>
</body>
</html>