β Learn Javascript
Use form data via the Document Object Model (DOM)
slides | md
Review the following sections and perform the activities on your own or with your group. Perform the task(s) when you see this π emoji
Students who complete this module will be able to:
document
window
{}
f
alert(123)
window.alert(123)
window.document
// get property window.document.body.style.background // set the property document.body.style.background = "red" // get web page url property window.document.URL // set a property document.location = "https://davidson.edu" // replace the content of the body document.body.innerHTML = "π"
document.querySelector()
<p id="greeting">Hello world</p>
// store reference to element let ele = document.querySelector("#greeting"); // get the text console.log(ele.textContent); // or set the text ele.textContent = "Hello world!";
Red
Green
Blue
<p style="color:red">Red</p> <p style="color:green">Green</p> <p style="color:blue">Blue</p>
// get array of all <p> elements on a page let eles = document.querySelectorAll("p"); // use a loop to log them eles.forEach(function(ele){ console.log(ele) })
π In codepen create <form> and <input> elements and add a submit listener.
<form>
<input>
submit
<form action="#" class="myForm"> <input type="text" id="color"> <button type="submit">Submit</button> </form>
// get reference let myForm = document.querySelector(".myForm"); // listens for when the form was submitted myForm.addEventListener("submit", function(event) { console.log("Form was submitted!"); })
preventDefault
event
this
.color
.value
// get reference let myForm = document.querySelector(".myForm"); // listens for when the form was submitted myForm.addEventListener("submit", function(event) { event.preventDefault(); document.body.bgColor = this.color.value; })
Follow the book's prompt to make the form for Usability Test...
<form action="#" class="myForm"> <input type="number" id="q1"> <input type="number" id="q2"> <button type="submit">Submit</button> <input type="number" id="answer"> </form>
myForm.addEventListener("submit", (event) => { event.preventDefault(); let q1 = Number(this.q1.value); let q2 = Number(this.q2.value); // add all converted responses let answer = (q1 + q2); // store in a new input and log to the console this.answer.value = answer; console.log(answer); });
π Try it out
jQuery is a Javascript library you will see often in code references. It provides easy access to properties and methods in the HTML DOM, but it is better to learn "pure JS" (a.k.a. "vanilla").
NOTE: youmightnotneedjquery.com
// select all divs, set backgrounds red $('div').css({ 'background': 'red' }); // select all elements with myClass, add text to end $('.myClass').append(' - here is some new text');
Overview: Events and using forms with Javascript and jQuery
// click event with callback $('#submitButton').click(function() { // set the value of a form element $('#color').val("purple"); // do not perform the default form action return false; });
Presentation comments ...
--- ## Contents 1. [Introduction](#introduction) 1. [Selecting HTML elements](#selecting-html-elements) `5 min` 1. [Next steps](#next-steps) 1. [Exercises](#exercises) 1. [References](#references)