Overview: Today we will cover more advanced selection, events, event listeners, as well as inputs. All of these things often relate to each other, so lets get started!
B | I | N | G | O |
---|---|---|---|---|
A1 | A2 | A3 | A4 | A5 |
B1 | B2 | Free! | B4 | B5 |
C1 | C2 | C3 | C4 | C5 |
See the Pen eebWXW by Mike Parda (@mikeparda) on CodePen.
See the Pen EbdLzm by Mike Parda (@mikeparda) on CodePen.
tdwould be, and go from there), but this still would leave us with issues when trying to select and black out other squares.
.find()
$('[data-id="someId"]')
$('div').not('.dontSelect');
$('#parent').children();
$('#child').parent();
$('#element').next();
document.querySelectorAll()
document.querySelectorAll('[data-id="someId"]');
document.querySelectorAll('div:not(.dontSelect)');
document.getElementById('parent').children;
document.getElementById('child').parentNode;
document.getElementById('element').nextElementSibling;
querySelectorAll?
querySelectorAllreturns an array of matching results, which is something to keep in mind when dealing with it. Simply add a function called
$to the
windowand you can make a bad version of jQuery in a few lines of code, which actually is not too bad for simple selections:
See the Pen bYOrGr by Mike Parda (@mikeparda) on CodePen.
keyCodeis an integer which represents the key pressed (EX: 13 === 'Enter' key) NOTE: This property only comes with KeyboardEvents
targetThis property tells us the element that the event was targeting (EX: handling a click event for a button, the button itself will be the target)
typeThis property is the actual event type (NOT the event object type, but the type of event, such as
keydownor
click).
defaultPreventedThis lets us know if the default browser behavior for this type of event has been stopped (more on this below).
cancelBubbleThis property lets us know if the event is going to bubble up or not to any parents (more on this below)
event.preventDefault()and
event.stopPropagation()
defaultPreventedand
cancelBubblecan be changed with these functions to stop events from acting in how the browser would normally let them happen.
<form>with a
<button type="submit">, since the type of the button is "submit", the browser will attempt to submit the form. NOTE: if you have a button inside of a form, unless it is specifically type="button", the browser will attempt to submit the form.
See the Pen rYoGNV by Mike Parda (@mikeparda) on CodePen.
stopPropagationwill stop the event from bubbling up. Wait... what? An example would be this scenerio: you have 2 elements, both with click events, but one is nested inside the other. If you click on the inner element, the event will bubble up to the parent element, and trigger its event handler as well. Let's take a look at an example:
See the Pen wPRPLd by Mike Parda (@mikeparda) on CodePen.
See the Pen pdqpaq by Mike Parda (@mikeparda) on CodePen.
See the Pen LOMeab by Mike Parda (@mikeparda) on CodePen.
documentand pass in the selector of the element we want the event listener to be put on. Since the document is the DOM, it is always there, so we are gaurenteed to have our event attached somewhere. So if we just pass in the selector of the element we want to add the event listener to, jQuery will create the event listener on the document, but will only fire the event if the event was triggerd by the selector.
See the Pen VrqQeM by Mike Parda (@mikeparda) on CodePen.
See the Pen yPGKPz by Mike Parda (@mikeparda) on CodePen.
document.readyand that answer is not wrong. That is one way to make sure you do not run any JS before the document is loaded. But is it necessary?
<head>tag, that is an old standard that is not valid anymore. Including your scripts in the
<head>only ensures that you will need to use some sort of
document ready, since your scripts will load well before any of your HTML. That being said, you may find yourself working on an application that includes the scripts in the
<head>and there is nothing you can do to change it. Not a problem, let's look at a few ways to defer executing your JS until the HTML document is ready.
See the Pen QOzVKm by Mike Parda (@mikeparda) on CodePen.
See the Pen WXLgVL by Mike Parda (@mikeparda) on CodePen.
See the Pen POXyzB by Mike Parda (@mikeparda) on CodePen.
See the Pen wPRYRV by Mike Parda (@mikeparda) on CodePen.