Change a CSS class when a user clicks something.
var myElement = document.getElementById('#my-special-paragraph');
myElement.addEventListener('click', changeTextColor);
<img>
tag should make the image larger.Get all links in books and add the class important:
var books = document.getElementsByClass("book");
for (var i = 0; i < books.length; i++) {
var links = books[i].getElementsByTagName("a");
for (var j = 0; j < links.length; j++) {
links[j].classList.add("important");
}
}
$(".book a").addClass("important");
// Plain Javascript
var books = document.getElementsByClass("book");
for (var i = 0; i < books.length; i++) {
var links = books[i].getElementsByTagName("a");
for (var j = 0; j < links.length; j++) {
links[j].classList.add("important");
}
}
// JQuery (still JavaScript)
$(".book a").addClass("important");
jQuery element(s) = dollar sign + selector string in parentheses
var allImages = $('img');
var myBio = $('#bio');
$('#bio').addClass('gray-background');
jQuery()
.$('.urgent')
$('#bio')
$('div')
$('div.urgent')
$('footer a.fancy')
Doctor Smith Doesn’t Make Pretty Snowmen
<script src="https://code.jquery.com/jquery.js"></script>
<head>
tag.js
file extension..js
file after the jQuery script tag<!doctype html>
<html>
<head>
</head>
<body>
</body>
</html>
<!doctype html>
<html>
<head>
<!-- CSS HERE -->
<!-- JAVASCRIPT HERE -->
</head>
<body>
<!-- HTML HERE -->
</body>
</html>
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="stylesheet.css">
<style>
.fancy { color: pink; display: none; }
</style>
<script src="http://code.jquery.com/jquery.js"></script>
</head>
<body>
<div class="fancy">I'm fancy!</div>
<script>
$(".fancy").show();
</script>
</body>
</html>
$("section").addClass("important"); // no error if already there
$("section").hasClass("important"); // returns true or false
$("section").removeClass("important"); // no error if not there
$("section").toggleClass("important");
$("img#cat").attr("src"); // returns src attribute
$("img#cat").attr("src", "cat.jpg"); // add or change
<div>
<p>Existing Text.</p>
</div>
$("p").prepend("Hello ");
$("p").append(" The End!");
<div>
<p>Existing Text. The End!</p>
</div>
$("section").html(); // returns HTML
$("section").html("Hi"); // sets HTML
$("section").empty(); // remove children
$("section").remove(); // remove this element + children
$("img").hide();
$("img").show();
$("img").toggle();
$("img").slideUp(); // shrink into nothing in position
$("img").slideDown(); // grow into itself in position
$("img").slideToggle(); // toggles via sliding
fadeIn()
fadeOut()
fadeToggle()
$("img").slideUp(3000); // milliseconds (3 secs)
Is the following jQuery code allowed?
$("img#cat").attr("src", "cat.jpg").attr("height", 500);
// click event in Plain JavaScript
var myBtn = document.getElementById('special-btn');
myBtn.addEventListener('click', alertMe);
// click event in JavaScript using JQuery
$('#special-btn').on('click', alertMe);
My helper function (no JQuery here):
function alertMe(evt) {
alert("Don't click that, please.");
}
$('#special-btn').on('mouseover', alertMe);
$('input').on('change', alertMe);
$('form').on('submit', alertMe);
$('input').on('keypress', alertMe);
<img id="cat" src="cat.jpg" alt="A cute kitten">
.special-border {
border-color: green;
border-radius: 4px;
border-style: solid;
}
$("#cat").on("mouseover", showBorderOnKitten);
Helper function (there is JQuery here):
function showBorderOnKitten(evt) {
$("img#cat").addClass("special-border");
}
$(".trigger-alert").on("change", function (evt) {
alert("Don't do that, please.");
}
);
-exercise time!