0

I'm currently having trouble adding a class to an element on click. When clicked, the Yes / No buttons are supposed to turn red. However this does not happen. No errors are logged into the console log either. Here is the JSFiddle. I have attempted to write the function in different ways to no avail. For some reason, when only the buttons are on the page, the classes are successfully added. I have trailed through the forum but cannot find a solution.

window.onload = () => {

  var yesButton = document.getElementById("yes-button");
  var noButton = document.getElementById("no-button");


  yesButton.onclick = () => {
    yesButton.classList.add("buttonActive");
    noButton.classList.remove("buttonActive");
  }

  noButton.onclick = () => {
    noButton.classList.add("buttonActive");
    yesButton.classList.remove("buttonActive");
  }

}
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.buttonActive {
  background-color: red;
}

.page-wrapper {
  width: 1075.20px;
  margin-left: 10%;
  margin-right: 10%;
}

#main-header {
  height: 60px;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #0b0c0c;
}

#header-wrap {
  width: 1075.20px;
  margin-left: 5%;
  margin-right: 5%;
}

#logo {
  width: 160px;
}

#heading-container {
  height: 205px;
  width: 100%;
  background-color: #0b0c0c;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
}

#heading-container h1 {
  font-size: 3em;
  max-width: 537.60px;
  font-family: 'Roboto', sans-serif;
}

#section-1 {
  background-color: #272828;
  display: flex;
  justify-content: center;
  padding-top: 50px;
  padding-bottom: 50px;
  color: white;
  font-family: Roboto;
  font-size: 1.2em;
}

#section-1-wrapper ul {
  max-width: 537.60px;
  list-style-position: inside;
}

#section-1 p {
  margin-bottom: 12px;
}

#section-1 p:nth-child(1) {
  font-weight: 700;
}

#section-1 li {
  margin-bottom: 12px;
  text-align: left;
}

#section-2 {
  display: flex;
  justify-content: center;
  align-items: center;
  padding-top: 50px;
  padding-bottom: 50px;
  font-family: Roboto;
}

#section-2-wrap {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

#section-2 h1 {
  font-size: 2em;
  margin-bottom: 50px;
}

#section-2 h2 {
  font-size: 1.5em;
  margin-bottom: 20px;
}

#button-div {
  margin-bottom: 20px;
}

#section-2 button {
  background-color: #2A8EBA;
  padding: 20px 50px;
  color: white;
  font-weight: 700;
  align-self: flex-start;
  flex-direction: column;
  border: 0;
}

#section-2 button:hover {
  cursor: pointer;
}

#question {
  width: 100%;
}

#section-2 input {
  width: 100%;
  max-width: 323px;
  padding: 12px;
  margin-bottom: 50px;
}
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css?family=Roboto:400,500,700,900&display=swap" rel="stylesheet">
<script src="https://kit.fontawesome.com/cd801faa65.js" crossorigin="anonymous"></script>
<header id="main-header">
  <header class="page-wrapper">
    <img src="images/logo.png" id="logo" alt="logo">
  </header>

</header>

<header id="heading-container">

  <div class="page-wrapper">
    <h1>HEADING</h1>
  </div>
</header>

<section id="section-1">
  <div class="page-wrapper">
    <p>PARAGRAPH ITEM</p>

    <ul>
      <li>LIST ITEM</li>
      <li>LIST ITEM</li>
      <li>LIST ITEM</li>
      <p>PARAGRAPH ITEM
      </p>
    </ul>
  </div>
</section>



<section id="section-2">
  <div id="section-2-wrap" class="page-wrapper">

    <h1>HEADING</h1>

    <h2 id="test">HEADING 2</h2>

    <div id="button-div">
      <button id="yes-button">YES</button>
      <button id="no-button">NO</button>
    </div>


    <h2>HEADING 2</h2>
    <input type="text"></input>



    <button>GO</button>




  </div>
</section>
Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
OJM
  • 315
  • 4
  • 13
  • 3
    It does get the class. You just need to read up on [CSS specificity](https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity). A class can never override an ID. ID = 100 points and class is only 1. – Lekoaf Apr 22 '20 at 11:43
  • 4
    The selector `#section-2 button` is more specific than `.buttonActive` and overrides the background colour. You could have seen this by examing the button in the browser's Developer Tools which would show the `#section-2 button` ruleset first and the background colour for `.buttonActive` crossed out. – Quentin Apr 22 '20 at 11:43
  • @Lekoaf The IDs cannot be compared to classes (101 classes still cannot override an ID) – FZs Apr 22 '20 at 11:52
  • @FZs not really a relevant point, since I hope nobody is stupid enough to try and add 101 classes to override an ID. Instead, change the ID to a class. – Lekoaf Apr 22 '20 at 11:59
  • @Lekoaf That's right... – FZs Apr 22 '20 at 12:00
  • Okay that now makes a lot of sense. So which ID would I now change to a class to ensure the new class is added? – OJM Apr 22 '20 at 12:03

0 Answers0