1 of 15

JavaScript and DOM Manipulation

CSCI 344: Advanced Web Technologies

Spring 2025

1

2 of 15

Announcements

  • My office hours today will be from 4:30-5:15PM (I have a conflict)

2

3 of 15

Outline

  • Strings and template literals
  • DOM functions and properties
  • Practice with templates, DOM functions, and loops

3

4 of 15

Outline

  • Strings and template literals
  • DOM functions and properties
  • Practice with templates, DOM functions, and loops

4

5 of 15

Strings and Template Literals

  • One of the most common tasks in JavaScript is to build HTML snippets by using string functions..
  • Why?
    • Because most of the content you display on your site comes from server data.
  • Because of this, you need to do a lot of string concatenation:
    • document.querySelector("div").innerText = "<p>" + name + "</p>";
  • Because concatenation is tedious, template literals were introduced in ES6 to make your life easier.

6 of 15

ES6 Introduced a better way: Client-side templates

  • Templates, or “template literals” are strings that allow you to embedded expressions
  • They’re convenient for generating larger chunks of HTML from lists of objects
  • Uses the “backtick” character (instead of regular single or double quotes) to indicate that you are specifying a template (above the tab key):

` <template goes here> `

  • Within the template, expressions represented like this:

${ my_expression }

6

7 of 15

More on templates

const name = 'Walter';

console.log( ` A template

can be multiple lines.

It can also evaluate expressions like:

${2 + 2} or� ${name} or

${getTodaysDate()}

` );

7

8 of 15

Example of Data → HTML Workflow

const player = {

name: "Jane",

pic: "http://website.com/avatar.png",

score: 300

};

<div class="card">

<img src="http://website.com/avatar.png">

<p>Jane scored 300 points</p>

</div>

The Data�(from server)

The HTML�(Goal)

9 of 15

You COULD do one big string concatenation...

...but string concatenation is annoying – difficult to read and easy to make mistakes.

const html = '<div class="card">' +

'<img src="' + player.pic + '">' +

'<p>' + player.name + ' scored ' +

player.score + ' points</p>' +

'</div>';

9

10 of 15

Rewriting previous HTML using template syntax

const html = `

<div class="card">

<img src="${player.pic}">

<p>

${player.name}'s high score is:

${player.score}

</p>

</div>`;

11 of 15

Outline

  • Strings and template literals
  • DOM functions and properties
  • Practice with templates, DOM functions, and loops

11

12 of 15

But how do you change the interface?

Once you build a string representation of some HTML…�

const name = "John";�const snippet = `<p>Welcome ${name}!</p>`;

�…you need to teach your program how to inject it into the DOM. There are a few ways to do this:

const myElement = document.querySelector("#my_container");

myElement.innerHTML = snippet; // replaces

myElement.insertAdjacentHTML('beforeend', snippet); //appends

12

13 of 15

Other DOM functions & properties

13

14 of 15

Outline

  • Strings and template literals
  • DOM functions and properties
  • Practice with templates, DOM functions, and loops

14

15 of 15

Practice Time

Download the Lecture 11 Code

15