So I’ve created a very basic practice page, where clicking each button will bring up a picture of the country. Below the picture is some text.
My question is, how do you go about making the text below the picture also change with each button? For example, changing from "This is a picture of Spain" to "My mum likes cooking" etc when you hit the different buttons.
I read somewhere you can have multiple onclick events with ; between them, but I’m very new to Javascript and haven’t been able to work out how to use multiple onclick events.
My HTML with script Javascript is below:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Birds</title>
<link rel="stylesheet" type="text/css" href="styling.css">
</head>
<body>
<main>
<h1>Places</h1>
<h2>Subheading Goes Here</h2>
<img id="bannerImage" src="https://www.planetware.com/wpimages/2020/03/portugal-in-pictures-beautiful-places-to-photograph-lisbon.jpg" /> <br> <br />
<p id ="text">This is a picture of Northern Ireland</p>
<nav>
<button class="mainbuttons" onclick="changeImage('https://www.planetware.com/wpimages/2020/03/portugal-in-pictures-beautiful-places-to-photograph-lisbon.jpg')">Northern Ireland</button>
<button class="mainbuttons" onclick="changeImage('https://theworldpursuit.com/wp-content/uploads/2021/01/things-to-do-in-northern-ireland.jpg')">Ireland</button>
<button class="mainbuttons" onclick="changeImage('https://d32uwaumftsuh7.cloudfront.net/Pictures/768x432/7/2/0/22720_gameofthronesthedarkhedges_thekingsroad_master_529527_crop.jpg')">Scotland</button>
<button class="mainbuttons" onclick="changeImage('https://media.cntraveller.com/photos/611bf776db797d0116fd53ab/master/w_1600,c_limit/causeway-coast-in-antrim-northern-ireland-gettyimages-1193546847.jpg')">Wales</button>
<button class="mainbuttons" onclick="changeImage('https://onhisowntrip.com/wp-content/uploads/2020/08/Great-British-Chefs-1.jpg')">Spain</button>
</nav>
<button id="themechanger" onclick="toggleTheme()">Toggle Theme</button>
<script>
function changeImage(fileName) {
let img = document.querySelector('#bannerImage');
img.setAttribute('src', fileName);
}
function toggleTheme(){
window.theme = typeof(window.theme)==='string' ? window.theme : 'root';
var switchToTheme = window.theme === 'root' ? 'dark' : 'root';
window.theme = switchToTheme;
document.querySelector('html').setAttribute('data-theme',switchToTheme);
}
</script>
</body>
</main>
</body>
</div>
</html>
I’ve tried various internet tutorials, but nothing seems to fit
2
Answers
There are a few ways to do this, but I think this is the easiest way with how you have it set up. I will explain the changes made and put the revised code at the bottom.
element where the text will be placed.
element to match the same sentence you have by default, then get the clicked elements inner text (name of country) and add it to the sentence.
The suggestion is to ..
data-*
global attributes and their counterpart properties on anHTMLElement
‘sdataset
propertybind
ing necessary data to a single event-handler implementation.The advantages are …