home..

Using Javascript with HTML DOM

Home HTML JavaScript DOM Data Types Correcting errors

Following along

Remember to “git pull” on teacher repository to update to lates.

Referencing HTML elements using javascript

%%html
<!-- the ID must be specified within the element -->
<h1 id="domTitleID">My Title</h1>

<!-- javascript goes here -->
<script>
var titleElement = document.getElementById("domTitleID")
<!-- outputs h1 tag -->
console.log("Example #1, show element in DOM")
console.log(titleElement)
</script>

Getting the data within the HTML element

%%html
<!-- the ID must be specified within the element -->
<h1 id="domTitleIDget">My Title</h1>

<!-- javascript goes here -->
<script>
var titleElement = document.getElementById("domTitleIDget")
<!-- outputs h1 innerHTML from h1 tag -->
console.log("Example #2, show innerHTML")
console.log(titleElement.innerHTML)
</script>

Setting the data within the HTML Element

%%html
<!-- the ID must be specified on the element -->
<h1 id="domTitleIDset">My Title</h1>

<!-- javascript goes here -->
<script>
var titleElement = document.getElementById("domTitleIDset")
titleElement.innerHTML = "Set and Update My Title"
<!-- outputs h1 innerHTML after h1 tag has been updated -->
console.log("Example #3, update innerHTML")
console.log(titleElement.innerHTML)
</script>

Creating elements

%%html
<!-- the ID must be specified on the element -->
<div id="divContainerID">
    <h1 id="h1ElementID">My Title</h1>
</div>

<!-- javascript goes here -->
<script>
   // creates a new element
   var pElement = document.createElement("p")
   pElement.innerHTML = "Starting a paragraph of text."
   
   // outputs p tag after it has been created
   console.log("Example #4, create a p tag within JS")
   console.log(pElement)
</script>

Issue! How to Create element that appears in HTML?

Solution

%%html
<!-- the ID must be specified on the element -->
<div id="divContainerIDset">
    <h1 id="h1ElementIDset">My Title</h1>
</div>

<!-- javascript goes here -->
<script>
   // creates a new element
   var pElement = document.createElement("p")
   pElement.innerHTML = "Starting a paragraph of text."
   
   // outputs p tag after it has been created
   console.log("Example #5, add p tag to HTML")
   console.log(pElement)
   
   // place the p element inside the HTML page
   var div = document.getElementById("divContainerIDset")
   div.appendChild(pElement)
</script>

Functions in JavaScript, using with DOM

Creeating functions

%%html
<!-- the ID must be specified on the element -->
<div id="divContainerIDfunction">
    <h1 id="h1ElementIDfunction">My Title</h1>
</div>

<!-- javascript goew here -->
<script>
    // define a function => takes parameter text, returns a new p tab
    function createPTag(text) {
        // creates a new element
        var pElement = document.createElement("p")

        // using the parameter like a variable
        pElement.innerHTML = text
        
        // outputs p tag after it has been created
        console.log("Example #6, add p tag using a function")
        console.log(pElement)

        return pElement;
    }

    // using a function to create p tag
    var pTag = createPTag("Starting a paragraph with cooler text than before.")

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

OnClick Event

%%html
<!-- the ID must be specified on the elements -->
<button id="buttonID">Click here!</button>

<div id="divContainerIDbutton">
    <h1 id="h1ElementIDbutton">My Title</h1>
</div>

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

        // using the parameter like a variable
        pElement.innerHTML = text
        
        // outputs p tag after it has been created
        console.log("Example #7.1, add p tag using a function")
        console.log(pElement)

        return pElement;
    }

    // create a function that sets specific text and adds to div
    function addPTagOnButton() {
        // using our new function
        var pTag = createPTag("Starting a paragraph with text created on button press.")

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

        // add p tag to the div
        div.appendChild(pTag)
        
        // outputs p tag after it has been created
        console.log("Example #7.2, update container adding a 'p' tag")
        console.log(div)
    }

    // add the P tag when our button is clicked
    var myButton = document.getElementById("buttonID")
    myButton.onclick = addPTagOnButton
    
</script>

Hacks

%%html
<html>
  <head>
    <script>
      function swapLinks() {
        var link1 = document.getElementByID("link1");
        var link2 = document.getElementByID("link2");

        var text1 = link1.textContent;
        var text2 = link2.textContent;

        link1.href = href2;
        link2.href = href1;
        link1.textContent = text2;
        link2.textContent = text1;
      }
    </script>
  </head>
  <body>
    <header>
      <div id="titles">
        <h2>Demo Website</h2>
        <h4>A website to host coding projects.</h4>
      </div>
    </header>

    <p>
      <div class="text">
        <span class="paragraph" size=1>This is a sample paragraph</span><br>
        <span class="paragraph"></span><br>
        <span class="sentence" size=2>This is a sentence.</span>
        <br>
        <button id="random-button">Press to switch links</button>
      </div>
    </p>

    <p>
      <div class="links">
        <a id="link1" href="https://www.w3schools.com/">W3 Schools</a>
        <a id="link2" href="https://chat.openai.com">ChatGPT</a>
      </div>
    </p>
    <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Google_2015_logo.svg/368px-Google_2015_logo.svg.png"/>
    <br>
    <img src="https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fwww.computertechreviews.com%2Fwp-content%2Fuploads%2F2020%2F05%2FModern-Web-Design-Trends-1200x675.jpg&f=1&nofb=1"/>
    <br>
    <br>
  </body>
</html>

Demo Website

A website to host coding projects.

This is a sample paragraph

This is a sentence.




© 2024    •  Powered by Soopr   •  Theme  Moonwalk