JavaScript and DOM Manipulation
CSCI 344: Advanced Web Technologies
Spring 2025
1
Announcements
2
Outline
3
Outline
4
Strings and Template Literals
ES6 Introduced a better way: Client-side templates
` <template goes here> `
${ my_expression }
6
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
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)
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
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>`;
Outline
11
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
Other DOM functions & properties
13
Outline
14
Practice Time
Download the Lecture 11 Code
15