<body>
<h2>Ordered List Example</h1>
</body>
Use JavaScript to create and append this ordered list to the body:
<head>
<script src="../jquery-3.7.1.js">
</script>
... other script tags.
</head>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js">
</script>
... other script tags.
</head>
$(selector).action( );
// Hide all elements with node name p.
$("p").hide( );
// Change the value of the input
// controls with class="test" to "abc".
$(".test").val("abc");
// Change the inner html of the paragraph
// with id="p1" to "Hello, World!".
$("#p1").text("Hello, World!");
window.addEventListener("load", init);
is$(document).ready(init);or even shorter
$(init);
<!DOCTYPE html>
<html lang="en">
<head>
<script src="../jquery-3.7.1.js">
</script>
<script src="myscript.js">
</script>
<link rel="stylesheet"
href="styles.css">
</head>
<body>
<h1>jQuery Examples -- Set 1</h1>
<button id="btn1">Show Greeting</button>
<p id="p1"></p>
</body>
</html>
Test these two choices for myscript.js, then translate them into vanilla JavaScript.
// myscripta.js
$(document).ready( ( ) => {
$("#p1").text = "Hello World!";
});
// myscriptb.js
$(document).ready( ( ) => {
${"btn1").click( ( ) => {
$("p1").text = "Hello World!";
});
});
// myscripta.java translated into
// vanilla JavaScript
document.addEventListener("DOMContentLoaded", ( ) => {
document.querySelector("p").innerHTML =
"Hello, World!";
});
// myscriptb.java translated into
// vanilla JavaScript
document.addEventListener("DOMContentLoaded", ( ) => {
document.querySelector("btn1").
addEventListener("click", ( ) => {
document.querySelector("p").innerHTML =
"Hello, World!";
});
});
$(init);is written in vanilla JavaScript as
window.addEventListener("load", init);
or
window.onclick = init;
// jQuery Script
$(( ) => {
$("#p1").text("Hello, World!");
});
// The vanilla JavaScript translation is
window.onclick = ( ) => {
document.querySelector("#p1").innerHTML = "Hello, World!";
};
// jQuery Script
$(( ) => {
$("#btn1").click(( ) => {
$("#p1").text("Hello, World!");
});
});
// The vanilla JavaScript translation is:
window.onload = ( ) => {
document.querySelector("#btn1").onclick = ( ) => {
document.querySelector("#p1").innerHTML = "Hello, World!";
};
};
// or
window.addEventListener("load", ( ) => {
document.querySelector("#btn1").
addEventListener("click", ( ) => {
document.querySelector("#p1").innerHTML = "Hello, World!";
});
});
| jQuery | JavaScript |
|---|---|
| $("#p1") | document.querySelector("#p1"); document.getElementById("p1"); |
| $(".red") | document.querySelectorAll(".red"); document.getElementsByClassName("red"); |
| $("p") | document.querySelectorAll("p"); document.getElementsByTagName("p"); |
| $("ol ol") | document.querySelectorAll("ol
ol"); No equivalent document.getElements statement |
Look at some of the examples in the jQuery Tutorial:
www.w3schools.com/jquery/default.asp.
Translate these jQuery function calls from jQuery into vanilla JavaScript. Also, write the JavaScript functions in arrow notation.
$(function( ) {
alert("Hello, World!");
}
or
$(( ) => {
alert("Hello, World!");
});
$(function( ) {
$("btn1").click(function( ) {
alert("Hello, World!");
});
});
or
$(( ) => {
$("bt1").click(( ) => {
alert("Hello, World!");
});
});
$(function( ){
$("p").dblclick(function( ){
$(this).hide( );
});
});
Translate it into vanilla JavaScript. Answer:
function eventHandler(e) {
var para = e.target;
para.style.display = "none";
}
function init( ) {
var para = document.querySelectorAll("p");
for(let node of para)) {
node.addEventListener("dblclick",
eventHandler);
}
}
window.addEventListener("load", init);
Here is the script written with anonymous functions using arrow notation:
window.addEventListener("load", ( ) => {
var para = document.querySelectorAll("p");
for(let node of Array.from(para)) {
node.addEventListener("dblclick", e => {
var para = e.target;
para.style.display = "none";
});
}
});
mouseenter mouseleave mousedown mouseup hover focus blur
$("#p1").hover(function( ) {
alert("Welcome! You entered p.");
},
function( ) {
alert("Bye! You left p.");
});
Here is the Hover Example translated into JavaScript:
// There is no hover event in vanilla JavaScript.
// The mouseenter and mouseleave event handlers
// must be implemented separately.
window.addEventListener("load", ( ) => {
var para1 = document.querySelector("p");
para1.addEventListener("mouseenter", ( ) => {
alert("Welcome! You entered p.");
});
para1.addEventListener("mouseleave", ( ) => {
alert("Bye! You left p.");
});
});
$("p").on("click", function( ){
$(this).hide( );
});
$("p").on({
mouseenter: function(){
$(this).css("background-color", "lightgray");
},
mouseleave: function(){
$(this).css("background-color", "lightblue");
},
click: function(){
$(this).css("background-color", "yellow");
}
});
Translate this event handler into vanilla JavaScript.
Answer:
window.addEventListener("load", ( ) => {
var para = document.querySelector("p");
para.addEventListener("mouseenter", ( ) => {
para.style.backgroundColor = "lightgray";
});
para.addEventListener("mouseleave", ( ) => {
para.style.backgroundColor = "lightblue";
});
para.addEventListener("mouseenter", ( ) => {
para.style.backgroundColor = "lightgray";
});
para.addEventListener("click", ( ) => {
para.style.backgroundColor = "yellow";
});
});