02. Local interactivity and external JS script
In this lesson, we create a new development branch, add a dropdown menu for selecting buildings, and separate the JavaScript logic into an external script.js file.
Introduction
Section titled “Introduction”Use Case Diagram
Section titled “Use Case Diagram”Diagram created with Mermaid
Project File Structure
Section titled “Project File Structure”At the end of this lesson, the project folder will contain three main files in the root directory:
Directorycampus-map/
index.htmlMain HTML page containing the SVG structure and selection menustyle.cssStylesheet for page layout, graphics, and building highlightsscript.jsJavaScript file containing the logic for selecting buildings and updating the DOM
Diagramma di Sequenza
Section titled “Diagramma di Sequenza”Diagram created with Mermaid
Component Diagram
Section titled “Component Diagram”Diagram created with Mermaid
Creating the lesson-2 Working Branch
Section titled “Creating the lesson-2 Working Branch”Before we start writing the new code, create a dedicated branch to isolate the changes for this lesson:
- Open the Source Control panel in VS Code (
Ctrl + Shift + G). - Click the current branch name in the lower-left corner (or at the top of the panel), then
select
Create New Branch.... - Enter the name of the new branch:
lesson-2, then press Enter.
Updating style.css (Hover Effects and the active Class)
Section titled “Updating style.css (Hover Effects and the active Class)”Add the visual rules for the mouse cursor and the highlight effect (yellow border and glow)
to the bottom of your style.css file when a building is selected:
/* style for building text labels */.label { font-size: 14px; font-weight: bold; fill: #ffffff; text-anchor: middle; dominant-baseline: middle; pointer-events: none;} /* part to add at the bottom of the file */ /* Hover effect on all building groups */ #layer-edifici > g { cursor: pointer; transition: opacity 0.2s ease; } #layer-edifici > g:hover { opacity: 0.85; } /* Style applied to the building SELECTED through JS */ .attivo rect { stroke: #facc15 !important; /* Bright yellow */ stroke-width: 5px !important; filter: drop-shadow(0px 0px 10px #facc15); }Creating script.js and using document.getElementById()
Section titled “Creating script.js and using document.getElementById()”Create a new file named script.js in the project’s root folder.
Inside it, add a function that uses document.getElementById()
to retrieve the SVG element corresponding to the selected building and apply the .attivo class:
// Function invoked when the dropdown menu changesfunction evidenziaEdificioDaMenu() { rimuoviSelezionePrecedente(); const selectMenu = document.getElementById("select-edificio"); const idSelezionato = selectMenu.value; if (idSelezionato) { // Use getElementById to find the SVG element in the DOM const elementoEdificio = document.getElementById(idSelezionato); if (elementoEdificio) { elementoEdificio.classList.add("attivo"); } }}
// Function invoked by clicking an SVG building directly on the mapfunction selezionaDaMappa (idEdificio) { const selectMenu = document.getElementById("select-edificio"); selectMenu.value = idEdificio; evidenziaEdificioDaMenu();}
// Service function to clear the previous selectionfunction rimuoviSelezionePrecedente() { const edificioAttivo = document.querySelector(".attivo"); if (edificioAttivo) { edificioAttivo.classList.remove("attivo"); }}Adding interactivity in index.html
Section titled “Adding interactivity in index.html”To make the map interactive in the second lesson, modify index.html by adding a control panel with a dropdown menu, connecting click events to the buildings, and including the external script.
-
Adding the control panel with the dropdown menu (
Section titled “Adding the control panel with the dropdown menu (<select>)”<select>)Add the control panel immediately before the
<svg>tag.index.html <h1>🗺️ University Campus Map</h1><!-- CONTROL PANEL WITH DROPDOWN MENU --><div style="margin-bottom: 15px; background: white; padding: 10px; border-radius: 8px; border: 1px solid #cbd5e1;"><label for="select-edificio"><strong>🔍 Highlight Building:</strong></label><select id="select-edificio" onchange="evidenziaEdificioDaMenu()" style="padding: 6px; border-radius: 4px; border: 1px solid #0284c7;"><option value="">-- Select a destination --</option><option value="edificio-uffici">Administrative Offices</option><!-- TODO: Add the other custom buildings on your campus here (for example, classrooms and laboratories) --></select></div><svg viewBox="0 0 800 600" width="800" height="600" xmlns="http://www.w3.org/2000/svg"> -
Adding onclick events to SVG buildings
Section titled “Adding onclick events to SVG buildings”To allow each building to respond to a mouse click, connect the
onclick="selezionaDaMappa(...)"attribute to the<g>groups for the individual buildings inside<g id="layer-edifici">.index.html <!-- LAYER 3: CAMPUS BUILDINGS --><g id="layer-edifici"><g id="edificio-uffici"><!-- Add the onclick attribute linked to the JavaScript function --><g id="edificio-uffici" onclick="selezionaDaMappa('edificio-uffici')"><rect x="70" y="80" width="100" height="140" fill="#1e3a8a" rx="6" /><text x="120" y="150" class="label">Offices</text></g><!-- Repeat for the other buildings... --><!-- TODO: Extend the onclick="selezionaDaMappa('building-id')" attribute to all other buildings in your SVG --></g> -
Connecting the external JavaScript file
Section titled “Connecting the external JavaScript file”Finally, immediately before the closing body tag (
</body>), add the reference toscript.js, which contains the control logic.index.html </g></g></svg><!-- Link to the external JavaScript file --><script src="script.js"></script></body></html>
Final Commit and Synchronization (Git / GitHub)
Section titled “Final Commit and Synchronization (Git / GitHub)”Now that Lesson 2 is complete and tested in the browser, save the work in the repository:
- Return to the
Source Controltab in VS Code. - Verify that the modified and newly created files appear:
index.html,style.css, andscript.js. - Write a clear commit message in the text box, for example: “Completed Lesson 2: Local interactivity, dropdown menu, and script.js”.
- Click the
Commitbutton. - Click
Publish Branch(orSync Changes) to upload the newlesson-2branch and its commits to your remote GitHub repository.
Summary
Section titled “Summary”Learning Check
Section titled “Learning Check”The answers to this page's quizzes are saved in your browser's local storage and remain stored between visits
What is the purpose of the <select> menu in the interactive map?
- Automatically display all buildings in 3D.
- Allow the user to choose the building to highlight.
Correct: the selected value is read by the JavaScript function to identify the corresponding SVG element.
- Connect the
script.jsfile to the HTML page.
Which HTML attribute detects a change in the dropdown selection?
-
onclick="evidenziaEdificioDaMenu()" -
onchange="evidenziaEdificioDaMenu()"Correct:
onchangeruns when the value of the<select>menu changes. -
src="script.js"
Which statement returns the page element corresponding to a specific id?
-
document.querySelectorAll() -
document.getElementById(idSelezionato)Correct:
getElementById()retrieves the SVG building selected by the user from the DOM. -
selectMenu.value = idEdificio
Which method adds the .attivo class to the selected building?
-
elementoEdificio.remove() -
elementoEdificio.style() -
elementoEdificio.classList.add("attivo")Correct:
classList.add()applies the class that activates the selected building’s CSS style.
What does the attribute onclick="selezionaDaMappa('edificio-uffici')" allow you to do?
- Remove the building from the map when it is clicked.
- Select the building and update its highlight when it is clicked on the map.
Correct: the click passes the building identifier to the JavaScript function, which also updates the menu.
- Create a new option in the
<select>menu.
What is the purpose of the rimuoviSelezionePrecedente() function?
- Remove all SVG buildings from the DOM.
- Temporarily disable the dropdown menu.
- Remove the
attivoclass from the previously selected building.Correct: this ensures that only one building is highlighted at a time.
Where is the external JavaScript file script.js linked in the HTML page?
- At the beginning of the
<head>section using a<style>directive. - Before the closing
</body>tag using<script src="script.js"></script>.Correct: the external link loads the JavaScript functions without inserting the code directly into the HTML.
- Inside the
viewBoxattribute of the<svg>element.
Quiz progress
0 / 0 questions answered (0%)
0 correct answers
Your score
0%
0 / 0 correct answers
Answered: 0 / 0 (0%)
STICS © 2026 by Carlo Scocco is licensed under CC BY-NC 4.0