Home HTML JavaScript DOM Data Types Javascript Debugging

Following allong

  • Create a new markdown file in _posts (you will be able to put HTML in here)
  • You can try the concepts we are doing here on that post

Getting HTML elements in your javascript

  • To get an HTML element, use document.getElementById("the id here")
  • You will use the ID that you set in your HTML
  • if you console.log the resulting variable you will get some information about the element
%%html
<!-- the ID must be specified on the element -->
<h1 id="title1">My Title</h1>

<!-- our javascript goe here -->
<script>
var titleElement = document.getElementById("title1")
console.log("Example #1")
console.log(titleElement)
</script>

My Title

Basics of Objects

  • Our variable titleElement stores an “object”
  • Basically think of this as a group of data
    • Example “name” is a variable
    • BUT Student #10’s name is more specific, in this case Student #10 is the object
    • The name of Student #10 would be referred to as Student #10.name rather than name
  • To access a certain type of data from an “object” we use “.” notation
    • ie. To get the content of this titleElement, we would do titleElement.innerHTML
%%html
<!-- the ID must be specified on the element -->
<h1 id="title2">My Title</h1>

<!-- our javascript goe here -->
<script>
var titleElement = document.getElementById("title2")
console.log("Example #2")
console.log(titleElement.innerHTML)
</script>

My Title

Modifying the HTML

  • We can treat the data in this “object” (the html element) as a variable
    • Therefore, we can change the value using the “=” (assignment) operator
%%html
<!-- the ID must be specified on the element -->
<h1 id="title3">My Title</h1>

<!-- our javascript goe here -->
<script>
var titleElement = document.getElementById("title3")
titleElement.innerHTML = "A better title"
</script>

My Title

Functions with Objects

  • Functions allow you to “do something”
    • ex. “eat food”
  • We have used functions in a few of the examples
    • console.log = “print something”
    • document.getElementById = “find an element with id”
  • Functions take in parameters, what to do (inside the parenthesis)
    • ex. we have to tell console.log what to print
    • ex #2. we have to tell document.getElementById what the id of the elemt is
  • Functions can be used with objects as well
    • Example: doWork (who is doing the work)
    • With Objects: Student #10, doWork (Student #10.doWork())
    • The parenthesis after indicate it is a function (parameters can go in there as well)

Creating elements

  • We can create an element with the document.createElement function -> takes in the type of element
  • We can set properties in the element just like we did with the h1
%%html
<!-- the ID must be specified on the element -->
<div id="container1">
    <h1 id="title10">My Title</h1>
</div>

<!-- our javascript goe here -->
<script>
   // creates a new element
   var pElement = document.createElement("p")
   pElement.innerHTML = "some text"
</script>

My Title

Issue!

  • We don’t see the element
  • Here is a visualization of what is happening => the “p” is not placed inside the page! visual on p tag floating

Solution

  • We need to place the element somewhere
  • For example, we could add the element to the div
    • For this, we use the appendChild function on the div object (the parameter would be the p element we created)
    • Remember, we can use getELementById to get the object for something in the html (the div!)
  • Updated Diagram visual on p tag in div
%%html
<!-- the ID must be specified on the element -->
<div id="container2">
    <h1 id="title11">My Title</h1>
</div>

<!-- our javascript goe here -->
<script>
   // creates a new element
   var pElement = document.createElement("p")
   pElement.innerHTML = "some text"

   // place the p element in the webpage
   var div = document.getElementById("container2")
   div.appendChild(pElement)
</script>

My Title

Creeating functions

  • We were using functions for a lot of this functionality, but how can we create our own?
  • Useful to avoid writing the same code over and over again
  • We define parameters (they effectively become variables), and return “output” of the function
    • ie. getStudent(name); parameters = name; returns the actual student
  • See the example below
%%html
<!-- the ID must be specified on the element -->
<div id="container5">
    <h1 id="title15">My Title</h1>
</div>

<!-- our javascript goe here -->
<script>
    // create a function => takes in text, returns created p
    function createPTag(text) {
        // creates a new element
        var pElement = document.createElement("p")

        // using the parameter like a variable
        pElement.innerHTML = text

        return pElement;
    }

    // using our new function
    var pTag = createPTag("cooler text")

    // place the p element in the webpage
    var div = document.getElementById("container5")
    div.appendChild(pTag)
</script>

My Title

Reactivity

  • We can run functions when an event happens
    • In this case, we will add the p tag when the button is clicked
%%html
<!-- the ID must be specified on the elements -->
<button id="myButton">Click here!</button>

<div id="myContainer">
    <h1 id="myTitle">My Title</h1>
</div>

<!-- our javascript goe here -->
<script>
    // create a function => takes in text, returns created p
    function createPTag(text) {
        // creates a new element
        var pElement = document.createElement("p")

        // using the parameter like a variable
        pElement.innerHTML = text

        return pElement;
    }

    // create a function that sets specific text and adds to div
    function addCoolPTag() {
        // using our new function
        var pTag = createPTag("cooler text")

        // place the p element in the webpage
        var div = document.getElementById("myContainer")

        // add p tag to the div
        div.appendChild(pTag)
    }

    // add the P tag when our button is clicked
    var myButton = document.getElementById("myButton")

    // using the onclick variable of the button
    // functions can be used as values for these variables
    myButton.onclick = addCoolPTag
</script>

My Title

%%html
<style>
    .div{
        border: 2px solid black;
        background-color: white;
    }
    .p{
        color: black;
        text-align: left;
    }
    .button{
        color: black;
        background-color: grey;
        border: 1px solid, dimgrey;
        border-radius: 2px;
    }
    .a{
        color: blue;
        text-align: left;
    }
</style>

<div class="div">
    <p class="p" id="text">Hi</p>
    <br>
    <button class="button" onclick="Switch()">Switch the links</button>
</div>
<div class="div">
    <a class="a" id="link1" href="https://frogpants.github.io/student/">Spencer's Page</a>
    <br>
    <a class="a" id="link2" href="https://seannakagawa.github.io/student/">Sean's Page</a>
    <br>
    <a class="a" id="link3" href="https://zafeera123.github.io/ZafeerA123/">Zafeer's Page</a>
    <br>
    <a class="a" id="link4" href="https://trystan-schmits.github.io/Student1/">Trystan's Page</a>
    <br>
    <p class="p">Links to be switched ^</p>
</div>

<script>

    let current = 0;
    function Switch() {
        current = (current+1)%2;
        var link1 = document.getElementById("link1"); //get link1
        var link2 = document.getElementById("link2"); //get link2

        var link1Html = link1.innerHTML; //get current htmls
        var link2Html = link2.innerHTML;

        link1.innerHTML = link2Html; //switch them
        link2.innerHTML = link1Html;


        document.getElementById("text").innerText = "Switched! state:"+String(current); //update paragraph
    }

</script>

Hi