CC-BY-NC-4.0 / September 2019 / Loïc TRUCHOT
A JavaScript love story
in 2020
// dans la <head>
<script
src="https://code.jquery.com/jquery-3.5.1.min.js"
integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
crossorigin="anonymous">
</script>
John Resig choose to workout:
Why it becomes so popular ?
// before
var title = window.document.getElementById("page-title");
title.addEventListener("click", function(e) {
e = e || window.event;
var thisElement = e && e.target;
if (thisElement) {
var isBig = thisElement.style.textTransform === "uppercase";
thisElement.style.textTransform = isBig ? "capitalize" : "uppercase";
}
});
// after
$("#page-subtitle").click(function() {
var isBig = $(this).css("text-transform") === "uppercase";
$(this).css({ "text-transform": isBig ? "capitalize" : "uppercase" });
});
Not even complete !
// before
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://jsonplaceholder.typicode.com/posts/1");
xhr.onload = () => {
var response = JSON.parse(xhr.responseText);
window.document.getElementById("page-title").innerText = response.title;
};
xhr.onerror = () => console.log(xhr.statusText);
xhr.send();
// after
$.get("https://jsonplaceholder.typicode.com/posts/1").then(data => {
$("#page-subtitle").text(data.title);
});
Not even complete !
// get anything by css selector
$("body");
$("#any-id");
$(".any-class");
$("tr:nth-child(even) > td");
$("input[type='button']");
// append, prepend, remove
$("#any-id").remove();
const that = $(".any-class");
that.append("<img />");
$("body").append(that);
$("body").prepend($("div.test > img:nth-child(3)"));
// change any css
$("input[type='button']").css("font-size", "20px");
$("td.dark").css({
"background-color": "black",
color: "white"
});
git clone https://github.com/ltruchot/jquery-introduction.git
const $ = function(selector) {
let element = null;
switch (selector.charAt(0)) {
case "#":
return document.getElementById(selector.substr(1));
case ".":
return document.getElementsByClassName(selector.substr(1));
default:
break;
}
return element;
};
const test = $("#test");
console.log(test);
Really understand the dom master, from scratch...
var divEl = $('<div id="toto"></div>');
$("body").append(divEl);
// ou directement
$("#toto").append('<div id="titi"></div>');
$("#toto").prepend('<div id="tutu"></div>');
// after, before
$('<div id="tete"></div>').appendTo("#tutu");
console.log($("#tutu").text(), $("#tutu").html());
$("#tutu").html("<span>aurevoir</span>");
console.log($("#tutu").text(), $("#tutu").html());
$("#tutu > span").text("adieu");
console.log($("#tutu").text(), $("#tutu").html());
$("#toto").remove();
$("#tutu").empty();
alternative...
Tamagochi revival
$('#tutu').find("span");
console.log($("span").parent());
$("span").closest("div");
console.log($("#tutu").siblings())
var newTutu = $('#tutu').clone();
$("body").append(newTutu);
$("#toto").clone().appendTo("#tutu");
$('#toto > div').each(function () {
$(this).text("coucou");
});
console.log($("h1").css("font-size"));
$("h1").css("font-size", "12px");
$("h1").css({"font-color": "blue", "text-transform": "uppercase"});
console.log($("input").attr("type"));
$("input").attr("type", "number");
$("<img />").attr({
src: "/resources/hat.gif",
alt: "jQuery Logo"
});
$("h1").addClass("hidden");
L'aquarium
En 1202, Fibonacci décrit l'évolution d'une population de lapins. Il remarque que chaque mois leur nombre augmente: pour chaque couple de lapin fertile existant, il y a un nouveau couple qui naît... et qui sera fertile dans 1 mois !
Les lapins de Fibonacci
$("h1").hide();
$("h1").fadeIn(3000);
$("h1").animate(
{"margin-top": "100px"},
1000
);
$("h1").fadeOut("fast", function() {
$(this).show()
});
🠣 Alternative 🠣
Listen regular JS events....
...and even more (hover, toggle)
With 3 listener options :
on, off, one
Know your JS events and you know everything
You can do anything you want
$.ajax
$.get
$.post
$.ajax({
contentType: 'application/json',
url: 'https://mini-json-server.committers.ngo/livres',
dataType: 'json',
data: JSON.stringify(data),
type: 'POST',
})
.then(() => $.get('https://mini-json-server.committers.ngo/livres'))
.then(console.log);
La bdthèque
import "./styles.scss";
import $ from "jquery";
import drag from "jquery-ui/ui/widgets/draggable";
import drop from "jquery-ui/ui/widgets/droppable";
$("#drag").draggable();
$("#drop").droppable({
drop: function(event, ui) {
ui.draggable.empty();
$(this).css("background-color", "red")
}
});
<body>
<div id="drag">drag</div>
<div id="drop">drop</div>
</body>
You can do anything you want
import 'summernote';
<script src="jquery.flot.js"></script>
THANK'S EVERYONE
It's finally done ! See you soon