I took a break from work so today was mostly relaxing, doing fun activities, had a martial arts class, and some coding. I continued on the Case Converter project and studied DOM Methods.
DOM Methods
JS provides built-in functions to retrieve elements from web-page and make web pages more interactive.
getElementById()
If HTML element has an id
attribute, it can be retrieved as following:
<p id="blue-text">What's your hyper skill?</p>
<script>
let element = document.getElementById("blue-text"); // get the element by id
</script>
From here, the element can be used in functions, change its content, styles, move it on the page, and more.
element.style.color = "blue";
querySelector()
With the querySelector()
method it returns the first document element that corresponds to the specified selector:
<p>What's your hyper skill?</p>
<script>
let element = document.querySelector("p"); // get the element by selector
</script>
This method allows getting an element for further work on any selector or their group, not just on the element selector.
Please Note: querySelector returns only one element corresponding to the request.
querySelectorAll()
This method gets all the elements that match the specified selector:
<p>Tell me</p>
<p>What's your hyper skill?</p>
<script>
let elements = document.querySelectorAll("p"); // get elements by selector
</script>
querySelectorAll()
, unlike the previous method, returns a NodeList
. To create the handler, forEach
loop method is used.
There are class selectors (".className"
), id selectors ("#idName")
, and many others. All of them can be used in the methods of querySelectorAll()
and querySelector()
.
Quiz
-
Declare a mutable variable with the name restaurant
. Specify all elements with the menu
class received through the document
node as its value
-
Which one of these are correct ways to get Elements by id
?
-
document.getElementById(myHeader);
-
document.getElementById(“myHeader”);
-
document.querySelector(“#myHeader”);
-
document.querySelector(#myHeader);
addEventListener()
Previously, I talked about DOM events. These events can be processed via the listener method.
Each browser event has an event handler : a code block that occurs after the event operation. The addEventListener()
method registers the event handler for the target object, for which it will be called when the event occurs. The target object can be an HTML element, a document
, or any other object that supports events.
document.addEventListener("click", function() {
console.log("There's been a browser event");
});
In this example, addEventListener()
is used to create a click
event handler for the document
object.
The same method can be applied to HTML tags as follows:
document.getElementById("myBtn").addEventListener("keypress", function() {
// body
});
The code can be simplified using a variable:
let element = document.getElementById("myBtn");
element.addEventListener("keypress", function() {
// body
});
Tomorrow will continue part 2 of the case converter project. Until tomorrow…