Exploratory assignment 1
- Go to a random website
- Use inspect
- Create a DOM tree for the page that shows at least 6 layers.
- Keep the page open a little while longer...
MDN describes the DOM as:
"... a programming interface for HTML and XML documents. It represents the page so that programs can change the document structure, style, and content. The DOM represents the document as nodes and objects. That way, programming languages can connect to the page."- MDN contributors (2020) Introduction to the DOM [online] available from https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction [23 January 2020]
When you inspect a page, the HTML is shown in a DOM tree that you can unfold to explore the different levels of the tree. It may look something like this:
The document is an object, and each object can have some methods and properties attached.
One of the methods that is widely used, is the .getElementById() method which is used to access a specific element using it's id as selector.
Let's have a look at an example:
document is the object that we accessing, meaning the content on the page, and when we have access to that, we look for a specific element within the page - the one with id="myHeading". Notice how the method is written in camelCase - it won't work if it's not written that way.
The id that we are looking for is written inside the parentheses of the method and in quotation marks (either single or double).
Once we have accessed the element, possibilities are nearly endless as we can...
Are there any elements in the Light Bulb assignment that it would make sense to access and store this way? (Could be elements that we are interacting with or elements that we are changing in some way).
How would you access them?
Ok, so now that we have accessed the elements - how do we manipulate them?
Once we have accessed the element, we may look at what's inside it by using the innerHTML property:
Notice that in this example, instead of typing the whole thing (document.getElementById("myHeading").innerHTML) we use the name of the variable that we created.
Doing it this way enables us to read the content of the element, but we can also change it by assigning a new value to it:
In this example, we assign a new value to our element's innerHTML property; the string "Hello Spring!".
Notice that we're no longer just logging to the console, but that we're actually changing the content on the page.
Are there any elements in the Light Bulb program whose innerHTML we are changing?
When and how do we change it/them? - is it based upon a condition or an action/event, or maybe both?
We may also change the style instructions of the element, accessing the .style property:
Notice that the .style property is followed by the CSS property that we wish to change: In the first statement, we change the value of the color property to white, and in the second one we change the background-color property to black.
Be aware that CSS properties with a dash are written in camelCase and NOT with a dash! This goes for all CSS properties that contain a dash.
NB: If you inspect the element, you will notice that the style instructions are added to the element inline - and so we're not changing the actual CSS document this way, just overruling the style instructions in the stylesheet.
Are there any elements whose style instructions we are changing in the Light Bulb program?
When and how? - is it based upon a condition or an action, or maybe both?
We now know how to access specific elements on the page and how to change them, but we still need to look into how to change them WHEN specific actions or events takes place. In this introduction course, we will have a look into one event that can trigger a response - or rather a function - being the mouse click event.
To trigger a function when a specific element is clicked, we add an event listener to the element:
Just like the h1 heading in the previous example, our button has an id. We use this id to access the element, while storing it in a variable. We then attach an event listener to that variable:
In the example above, we grab the button element by using its id as selector. We then define a function that will change the innerHTML of that button from the original "My Button" to "Clicked", and lastly we add an event listener to the button that listens for a click and when a click occurs, triggers - or calls - the function changeContent.
Are there any events in the Light Bulb program that will trigger a change on the page?
If so - see if you can add an event listener to the element or elements that trigger the change.
Applying the same principles as in the Light Bulb program, see if you can program the "Like" program (wireframe on Canvas).
The goal is to create a program that will act exactly like the one designed in XD.