Lab 3A: A little UX/I(e) - Button events in the Fire KingDOM¶
The HTM-Elements: Ava<tag>
the last Airbender¶
Remember, when you see tags that have an open
and /end
in HTML, like <body></body>
, they are referred to as elements, so for example:
content
, for example, the earth
element’s content is Toph
. Unfortunately, despite how exciting those elements are, the most common HTML element is the <div></div>
element, which is a generic container.
This is not an element: <link>
because there is not /link
to close it.
Elements can contain other elements, for example:
HereToph
is inside the earth
element, which is inside the kingdom
element.
Reminder that all tags can have attributes:
The DOM
(Document Object Model) how HTML elements are treated as objects
that can be manipulated by JavaScript. The DOM
has an API (Application Programming Interface) with JavaScript is used to interact with the elements.
The DOM
is a tree structure, which means that the document
is the root of the DOM tree, and all elements are children of the document
.
DOM Tree
Just think of the DOM
as the foundation of our HTML
house and the elements
are the rooms where we can use JavaScript appliances to interact with them. Anything outside the <html>
element (i.e. after the </html>
) is not part of the DOM
so you can’t use JavaScript to access it.
Making JavaScript interact with HTML-ements of the Air KingDOM!¶
Objective
Make a button that we can click on to fly to a location for each of the markers you made.
-
Add a new function to our
addMarker
function -
Create the function to add buttons to the
DOM
-
Add a function to move the map
Creating elements in the DOM
?!
To create HTML elements with JavaScript you need to use the createElement method.
Create the function to add buttons¶
Next we will add our new function. Notice how we are using the lat
,lng
,and title
from the addMarker
function? That’s why it was helpful to do step one first.
function createButtons(lat,lng,title){
const newButton = document.createElement("button"); // (1)!
newButton.id = "button"+title; // (2)!
newButton.innerHTML = title; // (3)!
newButton.setAttribute("lat",lat); // (4)!
newButton.setAttribute("lng",lng); // (5)!
newButton.addEventListener('click', function(){
map.flyTo([lat,lng]); // (6)!
})
document.getElementById("contents").appendChild(newButton); //(7)!
}
- Creates a new button
element
- Gives the button a unique
id
- Gives the button a
title
- Sets the
latitude
- Sets the
longitude
- Tells Leaflet where to
flyTo()
, which is the latitude/longitude - This targets the
id
where the buttons should be added to! In this case it is the div
with the idcontents
!
Call the createButtons()
in our addMarker
function¶
Remember, the only way functions work is if they are called, so the last step is to call the createButtons()
in our addMarker()
function.
function addMarker(lat,lng,title,message){
console.log(message)
L.marker([lat,lng]).addTo(map).bindPopup(`<h2>${title}</h2> <h3>${message}</h3>`)
createButtons(lat,lng,title); //(1)!
return message
}
- This is the line that calls our
createButtons()
function!
Try clicking the button on the webpage and it should fly to the location of that marker!
๐End of Lab 3A๐¶
This is the end of part 1 for today’s lab which you can use for Lab Assignment 3A.
Let’s change our html
and js
file names, to part1.js
and part1.html
to get ready for the second part of the lab.
Your final code Lab 3A
should look like the following: