I previously inquired about setting the font-weight to bold on a text element when selected. Thanks to @Eric, this has been successfully implemented! However, I am facing an issue where clicking on one text makes it bold, and then clicking on another text also makes it bold simultaneously.
How can I ensure that only one text element is bold at a time?
You can view my code on JSFiddle: http://jsfiddle.net/6XMzf/ or see below:
CSS:
/* CSS code goes here */
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Sample HTML Page</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<div id="container">
<p class="text-bold">Text 1</p>
<p class="text-normal">Text 2</p>
<p class="text-normal">Text 3</p>
</div>
<script>
const texts = document.querySelectorAll('p');
texts.forEach(text => {
text.addEventListener('click', function() {
texts.forEach(t => t.classList.remove('text-bold'));
this.classList.add('text-bold');
});
});
</script>
</body>
</html>