Senior Software Engineer, ** Intersection**
-
Welcome to Lecture 15! Here’s a brief rundown.

-
Let’s explore what can be done with vanilla js
-
Generally, we first select the DOM element we wish to manipulate and then we leverage NodeList object properties to make the manipulations themselves.
-
const div = document.querySelector('div')
div.style.border = '1px solid red';
querySelectorAll instead?-
const div = document.querySelector('div')
console.log(div.style.border);
-
Consider this documentation. Based on the docs, how could you access the computed width of a div that has a CSS width set to 100%?
-
The browser gives us access to the the classList property to handle class manipulation.
-
const div = document.querySelector('div'); // <div></div>
div.classList.add('test'); // <div class="test"></div>
div.classList.remove('test'); // <div></div>
div.classList.contains('test'); // false
Always prefer to use the classList property as an interface for class manipulation on DOM HTML elements.
-
We are able to use the getAttribute and setAttribute properties to manipulate DOM element attributes
// <a></a>
const a = document.querySelector('a');
a.setAttribute('href', 'https://www.google.com');
// ^^ <a href="https://www.google.com"></a>
console.log(a.getAttribute('href'));
// ^^ https://www.google.com
-
Question: How can we select class js-nested below?
<div>
<span class="js-nested"></span>
</div>
// either:
document.querySelector('.js-nested')
// OR:
const par = document.querySelector('div');
const nested = par.querySelector('.js-nested')
Advantage: for large HTML pages, there may be a performance boost to search within a DOM node vs entire page
-
const div = document.querySelector('div')
if (div.innerText) {
div.innerText = 'test'
}
else {
div.textContent = 'test'
}
👇👇👇
-
Consider this repo. Download and open the jQuery_review folder. Complete the problems from basicMethodsReviewVANILLA.js file.
-
There’s a good chance our syntax would have been less verbose and marginally easier to remember.
-
-
It’s important to realize that jQuery uses vanilla JavaScript and is written using vanilla JavaScript.
-
You might not always have access to or be able to use jQuery. For example, React discourages manipulating the DOM directly and from using third-party libraries like jQuery when you absolutely need to modify the DOM directly.
-
As a front-end engineer, you need to be able to manipulate the DOM with and without jQuery.
-
-
DOM API
DOM API, so similar functions are now in vanilla JavaScript-
-
-
-
-
TBF there aren’t that many good reason to use jQuery these days. However, as an engineer, you should keep tradeoffs in mind.
-
-
jQuery’s primary function is called jQuery(), also aliased under $().
It is exposed as a global variable on the window object when we include the library.
-
It does different things depending on the argument passed to it.
-
-
For today’s class, we’ll include jQuery from a CDN (content delivery network). There’s quite a few CDNs; we’ll go with Google’s:
https://developers.google.com/speed/libraries/#jquery
-
-
<ul class="special-list">
<li>First Child</li>
<li>Second Child</li>
</ul>
// returns a jQuery object containing
// all the `li` inside `.special-list`
$(".special-list li")
// jQuery(".special-list li")
-
-
The main use of the jQuery function is to select a group of elements. It’s a lot like querySelectorAll().
-
However, the jQuery function returns a jQuery object, an array-like collection of elements.
We only have access to jQuery methods on jQuery objects, not vanilla JavaScript objects, e.g. Nodelist returned from querySelectorAll().
-
jQuery object
DOM API element using the index-
// bracket notation
$(".special-list li")[0];
// get method*
$(".special-list li").get(0);
-
Please don’t worry about knowing every single DOM API method and every single jQuery method.
I recommend getting comfortable reading documentation. You’ll be reading the JavaScript documentation on MDN and the jQuery documentation on jQuery’s website, as well as using Google, regularly.
-
Their search functionality is poor, so use Google and prefix your search with “jQuery”.
-
Consider this repo. Download and open the jQuery_review folder. Complete the selectorsReview.js file.
We solved it before without jQuery , now solve again with jQuery. Then, solve basicMethodsReview.js, again this time with jQuery
-
We use event listeners, also called event handlers, to designate certain code to run based on events.
-
There are built-in events for things that happen on our page, e.g. click, hover, submit, page finished loading, etc.
-
Common events are: submit, click, mouseenter, mouseleave, load.
You can find a full list of events here.
-
element.addEventListener(type, listener);
-
We can attach a listener, a function to run when the event occurs, to an element on the page.
-
We can retrieve the element using the methods mentioned previously, e.g. document.querySelector().
-
<a href="https://www.google.com/" target="_blank" rel="noopener">
Click Me!
</a>
const anchorElem = document.querySelector("a");
// The parameter `event` is an object with information
// about the mouse click.
anchorElem.addEventListener("click", event => {
// There are some default actions associated
// with an element.
// For anchor tags, we're taken to a new page.
// We need to disable that default behavior
// using `preventDefault()`
event.preventDefault();
console.log("You clicked the anchor tag!");
});
Create the following files:
console.log out the contents of your input field.(No need to create a js folder for now, this is a quick exercise)
-
Consider this repo. Download and open the simple_events_review folder. Complete the ask in js/app.js file.
Solve twice, once without jQuery and then again with jQuery.
-
Consider this repo. Download and open the dragging folder. Think about how you would implement dragging functionality
Solve twice, once without jQuery and then again with jQuery.