skip to Main Content

The blue F turns into an actual amount of weight when you enter a number into the input field above.

The Two Functions Kg() and Lbs() are changing the class .dynamic which is where Kg or Lbs is being appended to the 8 divs that have the .dynamic class. Now that’s exactly what isn’t happening. When I select the divs with the .dynamic class, It adds Kg or Lbs (whatever button I press) to the first element with the .dynamic class.

How do I make it so that it appends that to all of the elements with the .dynamic class?

//Variables
const mercury = document.getElementById("mercury");
const venus = document.getElementById("venus");
const earth = document.getElementById("earth");
const mars = document.getElementById("mars");
const jupiter = document.getElementById("jupiter");
const saturn = document.getElementById("saturn");
const uranus = document.getElementById("uranus");
const neptune = document.getElementById("neptune");
const weight = document.getElementById("weight");

weight.addEventListener("input", Calc);

function Calc() {
  if (weight.value > 99999) {
    alert("Max Amount Of Numbers is 99999");
    weight.value = "";
  } else {
    var val = weight.value;
    console.log(val);
    var calculate = val * 0.38;
    calculate = Math.round(calculate);
    mercury.innerHTML = calculate;
    console.log(calculate);

    var val = weight.value;
    console.log(val);
    var calculate = val * 0.9;
    calculate = Math.round(calculate);
    venus.innerHTML = calculate;
    console.log(calculate);

    var val = weight.value;
    console.log(val);
    var calculate = val * 1;
    calculate = Math.round(calculate);
    earth.innerHTML = calculate;
    console.log(calculate);

    var val = weight.value;
    console.log(val);
    var calculate = val * 0.38;
    calculate = Math.round(calculate);
    mars.innerHTML = calculate;
    console.log(calculate);

    var val = weight.value;
    console.log(val);
    var calculate = val * 2.36;
    calculate = Math.round(calculate);
    jupiter.innerHTML = calculate;
    console.log(calculate);

    var val = weight.value;
    console.log(val);
    var calculate = val * 0.92;
    calculate = Math.round(calculate);
    saturn.innerHTML = calculate;
    console.log(calculate);

    var val = weight.value;
    console.log(val);
    var calculate = val * 0.89;
    calculate = Math.round(calculate);
    uranus.innerHTML = calculate;
    console.log(calculate);

    var val = weight.value;
    console.log(val);
    var calculate = val * 1.12;
    calculate = Math.round(calculate);
    neptune.innerHTML = calculate;
    console.log(calculate);
  }
}

function lbs() {
  let unit = document.getElementById("unit");
  if (unit == null) {
    let newElement = document.createElement("h3");
    newElement.setAttribute("class", "value");
    newElement.setAttribute("id", "unit");
    newElement.textContent = "Lbs";
    document.querySelector(".dynamic").appendChild(newElement);
  } else {
    if (unit.innerHTML == "Kg") {
      unit.innerHTML = "Lbs";
    }
  }
}

function kg() {
  let unit = document.getElementById("unit");
  if (unit == null) {
    let newElement = document.createElement("h3");
    newElement.setAttribute("class", "value");
    newElement.setAttribute("id", "unit");
    newElement.textContent = "Kg";
    document.querySelector(".dynamic").appendChild(newElement);
  } else {
    if (unit.innerHTML == "Lbs") {
      unit.innerHTML = "Kg";
    }
  }
}
@import url("https://fonts.googleapis.com/css2?family=Montserrat:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap");
@font-face {
  font-family: SpaceQuest;
  src: url(https://raw.githubusercontent.com/Lemirq/WODP/master/Fonts/SpaceQuest-yOY3.woff);
}

h1,
h2,
h3,
h4,
h5,
h6 {
  margin: 0;
}

 ::-webkit-scrollbar {
  width: 10px;
}


/* Track */

 ::-webkit-scrollbar-track {
  background: url(./dario-bronnimann-hNQwIirOseE-unsplash.jpg);
}


/* Handle */

 ::-webkit-scrollbar-thumb {
  background: rgba(59, 59, 59, 0.741);
  border-radius: 200px;
}


/* Handle on hover */

 ::-webkit-scrollbar-thumb:hover {
  background: rgb(255, 255, 255);
}

* {
  --c-light: #f4f4f4;
  --c-dark: #141414;
  --c-blue: rgb(10, 132, 255);
  --f-body: "Montserrat", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
  --trans-ease-in-out: all 0.2s ease-in-out;
  color: var(--c-light);
}

body {
  background-image: url(https://raw.githubusercontent.com/Lemirq/WODP/master/images/dario-bronnimann-hNQwIirOseE-unsplash.jpg);
  margin: 0;
  inset: 50px;
  font-family: var(--f-body);
}

a {
  color: var(--c-light);
  text-decoration: none;
}


/***** NAVBAR *****/

.nav-item {
  transition: var(--trans-ease-in-out);
}

.ext-link {
  cursor: alias;
}

.nav-item:hover {
  color: var(--c-dark);
  background-color: var(--c-light);
}

.nav-item:hover li {
  color: var(--c-dark);
}

.nav-item.icon-link:hover i {
  color: var(--c-dark);
}

.nav-item:not(:last-child) {
  margin-right: 20px;
}

navbar {
  display: flex;
  flex-direction: row;
  justify-content: end;
  align-items: center;
  padding: 20px;
}

.nav-item-container {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  margin: 0;
  padding: 0;
}

.nav-item {
  display: inline-block;
  list-style: none;
  padding: 10px;
  font-size: 20px;
  border-radius: 10px;
}

.gh-icon {
  font-size: 30px;
}


/***** End NAVBAR *****/


/***** Main *****/

#wrapper {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

h1 {
  font-family: SpaceQuest, sans-serif;
  font-size: 3.5rem;
}

.input-group {
  border: 2px var(--c-light) solid;
  border-radius: 10px;
  /* max-width: 400px; */
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  margin-top: 50px;
}

[type="number"]:focus {
  outline: none;
}

[type="number"] {
  font-size: 20px;
  padding: 5px;
  background-color: rgba(217, 217, 217, 0.2);
  border: none;
  font-family: var(--f-body);
  min-width: 280px;
}

.btn-form {
  font-size: 20px;
  padding: 5px;
  background-color: rgba(217, 217, 217, 0.2);
  border: none;
  transition: var(--trans-ease-in-out);
  cursor: pointer;
  font-family: var(--f-body);
}

.btn-form:hover {
  background-color: rgba(217, 217, 217, 0.4);
}

.btn-form:first {
  border-right: var(--c-light) 1px solid;
}

.input-group-text {
  background-color: rgba(217, 217, 217, 0.2);
  font-size: 17px;
  padding: 7px;
}


/***** End Main *****/


/***** CARDS *****/

.card-container {
  margin: 50px;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  align-items: center;
  grid-gap: 10px;
  width: calc(100vw - 200px);
}

.card {
  background-color: #141414;
  width: auto;
  height: auto;
  border-radius: 10px;
  padding: 30px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  box-shadow: 0 1rem 3rem rgba(0, 0, 0, 0.175);
}

.planet-info {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}

.planet {
  font-size: 50px;
  margin: 0;
  text-transform: capitalize;
}

.planet-img {
  width: 80px;
  height: auto;
  margin-right: 30px;
}

[src="./images/planets/Saturn.png"] {
  height: 79.25px;
  width: auto;
}

.weight {
  margin-top: 10px;
  text-transform: capitalize;
  font-size: 20px;
}

.weight::after {
  content: ":";
}

.divider {
  height: 1px;
  width: 100%;
  margin: 20px 0;
  background-color: var(--c-light);
}

.value {
  font-size: 60px;
  color: var(--c-blue);
}

.dynamic>.value:nth-child(2) {
  margin-left: 10px;
}

.dynamic {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}


/***** End CARDS *****/

.input-error {
  outline: 1px solid red;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>




<navbar>
  <ul class="nav-item-container">
    <a target="_blank" class="nav-item ext-link" href="https://lemirq.github.io">
      <li>Website</li>
    </a>
    <a target="_blank" class="nav-item icon-link ext-link" href="https://github.com/Lemirq">
      <li><i class="bi bi-github gh-icon"></i></li>
    </a>

  </ul>

</navbar>
<div id="wrapper">
  <h1 id="vs-h1">Your Weight On Different Planets</h1>
  <div class="input-group">
    <input id="weight" placeholder="Enter your Weight" type="number">
    <button id="kg" onclick="kg()" class="btn-form" type="button">Kg</button>
    <button id="lbs" onclick="lbs()" class="btn-form" type="button">Lbs</button>


  </div>

  <div class="card-container">
    <div class="card">
      <div class="planet-info">
        <img class="planet-img" src="./images/planets/Mercury.png" alt="EARTH">
        <h3 class="planet">mercury</h3>
      </div>
      <div class="divider"></div>
      <h4 class="weight">weight</h4>
      <div class="dynamic">
        <h3 id="mercury" class="value">F</h3>
      </div>
    </div>
    <div class="card">
      <div class="planet-info">
        <img class="planet-img" src="./images/planets/Venus.png" alt="EARTH">
        <h3 class="planet">venus</h3>
      </div>
      <div class="divider"></div>
      <h4 class="weight">weight</h4>
      <div class="dynamic">
        <h3 id="venus" class="value">F</h3>
      </div>
    </div>
    <div class="card">
      <div class="planet-info">
        <img class="planet-img" src="./images/planets/Earth.png" alt="EARTH">
        <h3 class="planet">earth</h3>
      </div>
      <div class="divider"></div>
      <h4 class="weight">weight</h4>
      <div class="dynamic">
        <h3 id="earth" class="value">F</h3>
      </div>
    </div>
    <div class="card">
      <div class="planet-info">
        <img class="planet-img" src="./images/planets/Mars.png" alt="EARTH">
        <h3 class="planet">mars</h3>
      </div>
      <div class="divider"></div>
      <h4 class="weight">weight</h4>
      <div class="dynamic">
        <h3 id="mars" class="value">F</h3>
      </div>
    </div>
    <div class="card">
      <div class="planet-info">
        <img class="planet-img" src="./images/planets/Jupiter.png" alt="EARTH">
        <h3 class="planet">jupiter</h3>
      </div>
      <div class="divider"></div>
      <h4 class="weight">weight</h4>
      <div class="dynamic">
        <h3 id="jupiter" class="value">F</h3>
      </div>
    </div>
    <div class="card">
      <div class="planet-info">
        <img class="planet-img" src="./images/planets/Saturn.png" alt="EARTH">
        <h3 class="planet">saturn</h3>
      </div>
      <div class="divider"></div>
      <h4 class="weight">weight</h4>
      <div class="dynamic">
        <h3 id="saturn" class="value">F</h3>
      </div>
    </div>
    <div class="card">
      <div class="planet-info">
        <img class="planet-img" src="./images/planets/Uranus.png" alt="EARTH">
        <h3 class="planet">uranus</h3>
      </div>
      <div class="divider"></div>
      <h4 class="weight">weight</h4>
      <div class="dynamic">
        <h3 id="uranus" class="value">F</h3>
      </div>
    </div>
    <div class="card">
      <div class="planet-info">
        <img class="planet-img" src="./images/planets/Neptune.png" alt="EARTH">
        <h3 class="planet">neptune</h3>
      </div>
      <div class="divider"></div>
      <h4 class="weight">weight</h4>
      <div class="dynamic">
        <h3 id="neptune" class="value">F</h3>
      </div>
    </div>
  </div>

</div>

4

Answers


    1. Select all elements with the .dynamic class by using querySelectorAll
    2. Iterate through the NodeList and append desired child to each node
    let dynamics = document.querySelectorAll(".dynamic")
    dynamics.forEach((ele) => {
      let newElement = document.createElement("h3");
      ele.appendChild(newElement);
    }
    
    Login or Signup to reply.
  1. The script must look something like this:

    <script>
    let elements = document.querySelectorAll(".dynamic")
    elements.forEach(el=>{
        let child = document.createElement('div')
        el.appendChild(child)
    })
    </script>
    
    Login or Signup to reply.
  2. This can be simplified quite a bit, the buttons’ innerHTML values match what you want in the new unit elements.

        <button id="kg" class="btn-form" type="button">Kg</button>
        <button id="lbs" class="btn-form" type="button">Lbs</button>
    

    Use JavaScript to add an event listener to both unit buttons that call the same function which will update the innerHTML of the unit headings to match the clicked button. When initializing the unit heading elements you should not give them all the same id but rather a common class and you must also append a cloned copy of the element or it will just move the element form one spot to the next:

    document.querySelectorAll('#kg, #lbs').forEach(ub => ub.addEventListener('click', units));
    
    function units(e) {
      let units = document.querySelectorAll(".value.unit");
      if (units.length == 0) {
        let newElement = document.createElement("h3");
        newElement.setAttribute("class", "value unit");
        document.querySelectorAll(".dynamic").forEach((dyn) => {
          // append a cloned copy to each, not the same newElement
          dyn.appendChild(newElement.cloneNode())
        });
        // re-run the query to find the newly added nodes
        units = document.querySelectorAll(".value.unit");
      }
      // set the content
      units.forEach(unit => unit.innerHTML = e.target.innerHTML);
    }
    
    //Variables
    const mercury = document.getElementById("mercury");
    const venus = document.getElementById("venus");
    const earth = document.getElementById("earth");
    const mars = document.getElementById("mars");
    const jupiter = document.getElementById("jupiter");
    const saturn = document.getElementById("saturn");
    const uranus = document.getElementById("uranus");
    const neptune = document.getElementById("neptune");
    const weight = document.getElementById("weight");
    
    weight.addEventListener("input", Calc);
    
    function Calc() {
      if (weight.value > 99999) {
        alert("Max Amount Of Numbers is 99999");
        weight.value = "";
      } else {
        var val = weight.value;
        console.log(val);
        var calculate = val * 0.38;
        calculate = Math.round(calculate);
        mercury.innerHTML = calculate;
        console.log(calculate);
    
        var val = weight.value;
        console.log(val);
        var calculate = val * 0.9;
        calculate = Math.round(calculate);
        venus.innerHTML = calculate;
        console.log(calculate);
    
        var val = weight.value;
        console.log(val);
        var calculate = val * 1;
        calculate = Math.round(calculate);
        earth.innerHTML = calculate;
        console.log(calculate);
    
        var val = weight.value;
        console.log(val);
        var calculate = val * 0.38;
        calculate = Math.round(calculate);
        mars.innerHTML = calculate;
        console.log(calculate);
    
        var val = weight.value;
        console.log(val);
        var calculate = val * 2.36;
        calculate = Math.round(calculate);
        jupiter.innerHTML = calculate;
        console.log(calculate);
    
        var val = weight.value;
        console.log(val);
        var calculate = val * 0.92;
        calculate = Math.round(calculate);
        saturn.innerHTML = calculate;
        console.log(calculate);
    
        var val = weight.value;
        console.log(val);
        var calculate = val * 0.89;
        calculate = Math.round(calculate);
        uranus.innerHTML = calculate;
        console.log(calculate);
    
        var val = weight.value;
        console.log(val);
        var calculate = val * 1.12;
        calculate = Math.round(calculate);
        neptune.innerHTML = calculate;
        console.log(calculate);
      }
    }
    
    document.querySelectorAll('#kg, #lbs').forEach(ub => ub.addEventListener('click', units));
    
    function units(e) {
      let units = document.querySelectorAll(".value.unit");
      if (units.length == 0) {
        let newElement = document.createElement("h3");
        newElement.setAttribute("class", "value unit");
        document.querySelectorAll(".dynamic").forEach((dyn) => {
          // append a cloned copy to each, not the same newElement
          dyn.appendChild(newElement.cloneNode())
        });
        // re-run the query to find the newly added nodes
        units = document.querySelectorAll(".value.unit");
      }
      // set the content
      units.forEach(unit => unit.innerHTML = e.target.innerHTML);
    }
    @import url("https://fonts.googleapis.com/css2?family=Montserrat:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap");
    @font-face {
      font-family: SpaceQuest;
      src: url(https://raw.githubusercontent.com/Lemirq/WODP/master/Fonts/SpaceQuest-yOY3.woff);
    }
    
    h1,
    h2,
    h3,
    h4,
    h5,
    h6 {
      margin: 0;
    }
    
     ::-webkit-scrollbar {
      width: 10px;
    }
    
    
    /* Track */
    
     ::-webkit-scrollbar-track {
      background: url(./dario-bronnimann-hNQwIirOseE-unsplash.jpg);
    }
    
    
    /* Handle */
    
     ::-webkit-scrollbar-thumb {
      background: rgba(59, 59, 59, 0.741);
      border-radius: 200px;
    }
    
    
    /* Handle on hover */
    
     ::-webkit-scrollbar-thumb:hover {
      background: rgb(255, 255, 255);
    }
    
    * {
      --c-light: #f4f4f4;
      --c-dark: #141414;
      --c-blue: rgb(10, 132, 255);
      --f-body: "Montserrat", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
      --trans-ease-in-out: all 0.2s ease-in-out;
      color: var(--c-light);
    }
    
    body {
      background-image: url(https://raw.githubusercontent.com/Lemirq/WODP/master/images/dario-bronnimann-hNQwIirOseE-unsplash.jpg);
      margin: 0;
      inset: 50px;
      font-family: var(--f-body);
    }
    
    a {
      color: var(--c-light);
      text-decoration: none;
    }
    
    
    /***** NAVBAR *****/
    
    .nav-item {
      transition: var(--trans-ease-in-out);
    }
    
    .ext-link {
      cursor: alias;
    }
    
    .nav-item:hover {
      color: var(--c-dark);
      background-color: var(--c-light);
    }
    
    .nav-item:hover li {
      color: var(--c-dark);
    }
    
    .nav-item.icon-link:hover i {
      color: var(--c-dark);
    }
    
    .nav-item:not(:last-child) {
      margin-right: 20px;
    }
    
    navbar {
      display: flex;
      flex-direction: row;
      justify-content: end;
      align-items: center;
      padding: 20px;
    }
    
    .nav-item-container {
      display: flex;
      flex-direction: row;
      justify-content: center;
      align-items: center;
      margin: 0;
      padding: 0;
    }
    
    .nav-item {
      display: inline-block;
      list-style: none;
      padding: 10px;
      font-size: 20px;
      border-radius: 10px;
    }
    
    .gh-icon {
      font-size: 30px;
    }
    
    
    /***** End NAVBAR *****/
    
    
    /***** Main *****/
    
    #wrapper {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
    }
    
    h1 {
      font-family: SpaceQuest, sans-serif;
      font-size: 3.5rem;
    }
    
    .input-group {
      border: 2px var(--c-light) solid;
      border-radius: 10px;
      /* max-width: 400px; */
      display: flex;
      flex-direction: row;
      justify-content: center;
      align-items: center;
      margin-top: 50px;
    }
    
    [type="number"]:focus {
      outline: none;
    }
    
    [type="number"] {
      font-size: 20px;
      padding: 5px;
      background-color: rgba(217, 217, 217, 0.2);
      border: none;
      font-family: var(--f-body);
      min-width: 280px;
    }
    
    .btn-form {
      font-size: 20px;
      padding: 5px;
      background-color: rgba(217, 217, 217, 0.2);
      border: none;
      transition: var(--trans-ease-in-out);
      cursor: pointer;
      font-family: var(--f-body);
    }
    
    .btn-form:hover {
      background-color: rgba(217, 217, 217, 0.4);
    }
    
    .btn-form:first {
      border-right: var(--c-light) 1px solid;
    }
    
    .input-group-text {
      background-color: rgba(217, 217, 217, 0.2);
      font-size: 17px;
      padding: 7px;
    }
    
    
    /***** End Main *****/
    
    
    /***** CARDS *****/
    
    .card-container {
      margin: 50px;
      display: grid;
      grid-template-columns: 1fr 1fr 1fr 1fr;
      grid-template-rows: 1fr 1fr;
      align-items: center;
      grid-gap: 10px;
      width: calc(100vw - 200px);
    }
    
    .card {
      background-color: #141414;
      width: auto;
      height: auto;
      border-radius: 10px;
      padding: 30px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      box-shadow: 0 1rem 3rem rgba(0, 0, 0, 0.175);
    }
    
    .planet-info {
      display: flex;
      flex-direction: row;
      justify-content: center;
      align-items: center;
    }
    
    .planet {
      font-size: 50px;
      margin: 0;
      text-transform: capitalize;
    }
    
    .planet-img {
      width: 80px;
      height: auto;
      margin-right: 30px;
    }
    
    [src="./images/planets/Saturn.png"] {
      height: 79.25px;
      width: auto;
    }
    
    .weight {
      margin-top: 10px;
      text-transform: capitalize;
      font-size: 20px;
    }
    
    .weight::after {
      content: ":";
    }
    
    .divider {
      height: 1px;
      width: 100%;
      margin: 20px 0;
      background-color: var(--c-light);
    }
    
    .value {
      font-size: 60px;
      color: var(--c-blue);
    }
    
    .dynamic>.value:nth-child(2) {
      margin-left: 10px;
    }
    
    .dynamic {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
    }
    
    
    /***** End CARDS *****/
    
    .input-error {
      outline: 1px solid red;
    }
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    
    
    
    
    <navbar>
      <ul class="nav-item-container">
        <a target="_blank" class="nav-item ext-link" href="https://lemirq.github.io">
          <li>Website</li>
        </a>
        <a target="_blank" class="nav-item icon-link ext-link" href="https://github.com/Lemirq">
          <li><i class="bi bi-github gh-icon"></i></li>
        </a>
    
      </ul>
    
    </navbar>
    <div id="wrapper">
      <h1 id="vs-h1">Your Weight On Different Planets</h1>
      <div class="input-group">
        <input id="weight" placeholder="Enter your Weight" type="number">
        <button id="kg" class="btn-form" type="button">Kg</button>
        <button id="lbs" class="btn-form" type="button">Lbs</button>
    
    
      </div>
    
      <div class="card-container">
        <div class="card">
          <div class="planet-info">
            <img class="planet-img" src="./images/planets/Mercury.png" alt="EARTH">
            <h3 class="planet">mercury</h3>
          </div>
          <div class="divider"></div>
          <h4 class="weight">weight</h4>
          <div class="dynamic">
            <h3 id="mercury" class="value">F</h3>
          </div>
        </div>
        <div class="card">
          <div class="planet-info">
            <img class="planet-img" src="./images/planets/Venus.png" alt="EARTH">
            <h3 class="planet">venus</h3>
          </div>
          <div class="divider"></div>
          <h4 class="weight">weight</h4>
          <div class="dynamic">
            <h3 id="venus" class="value">F</h3>
          </div>
        </div>
        <div class="card">
          <div class="planet-info">
            <img class="planet-img" src="./images/planets/Earth.png" alt="EARTH">
            <h3 class="planet">earth</h3>
          </div>
          <div class="divider"></div>
          <h4 class="weight">weight</h4>
          <div class="dynamic">
            <h3 id="earth" class="value">F</h3>
          </div>
        </div>
        <div class="card">
          <div class="planet-info">
            <img class="planet-img" src="./images/planets/Mars.png" alt="EARTH">
            <h3 class="planet">mars</h3>
          </div>
          <div class="divider"></div>
          <h4 class="weight">weight</h4>
          <div class="dynamic">
            <h3 id="mars" class="value">F</h3>
          </div>
        </div>
        <div class="card">
          <div class="planet-info">
            <img class="planet-img" src="./images/planets/Jupiter.png" alt="EARTH">
            <h3 class="planet">jupiter</h3>
          </div>
          <div class="divider"></div>
          <h4 class="weight">weight</h4>
          <div class="dynamic">
            <h3 id="jupiter" class="value">F</h3>
          </div>
        </div>
        <div class="card">
          <div class="planet-info">
            <img class="planet-img" src="./images/planets/Saturn.png" alt="EARTH">
            <h3 class="planet">saturn</h3>
          </div>
          <div class="divider"></div>
          <h4 class="weight">weight</h4>
          <div class="dynamic">
            <h3 id="saturn" class="value">F</h3>
          </div>
        </div>
        <div class="card">
          <div class="planet-info">
            <img class="planet-img" src="./images/planets/Uranus.png" alt="EARTH">
            <h3 class="planet">uranus</h3>
          </div>
          <div class="divider"></div>
          <h4 class="weight">weight</h4>
          <div class="dynamic">
            <h3 id="uranus" class="value">F</h3>
          </div>
        </div>
        <div class="card">
          <div class="planet-info">
            <img class="planet-img" src="./images/planets/Neptune.png" alt="EARTH">
            <h3 class="planet">neptune</h3>
          </div>
          <div class="divider"></div>
          <h4 class="weight">weight</h4>
          <div class="dynamic">
            <h3 id="neptune" class="value">F</h3>
          </div>
        </div>
      </div>
    
    </div>
    Login or Signup to reply.
  3. Minor Problems

    • There’s no such HTML element <navbar>, there’s <nav>.
    • There’s a block code hard-coded 8 times with the only difference between them is a number. Whenever code needs to repeat itself, we use some sort of iteration such as a for loop or an array method and the numbers would be passed as a single variable passed with every iteration.
    const gforce = [0.38, 0.9, 1, 0.38, 2.36, 0.92, 0.89, 1.12];
    for (let i=0; i < 8; i++) {
    var val = weight.value;
        console.log(val);
        var calculate = val * gforce[i];//<== that should be a variable
        calculate = Math.round(calculate);
        venus.innerHTML = calculate;
        console.log(calculate);
    }
    

    Also, the <input> that’s supposed to calculate lbs and kg doesn’t appear to convert kg to lbs or vice-versa.

    Details are commented in example

    // An array of objects - each object represents a planet
    const data = [{
        planet: 'Mercury',
        gforce: 0.38,
        img: `https://upload.wikimedia.org/wikipedia/commons/thumb/4/4a/Mercury_in_true_color.jpg/440px-Mercury_in_true_color.jpg`
      },
      {
        planet: 'Venus',
        gforce: 0.9,
        img: `https://upload.wikimedia.org/wikipedia/commons/thumb/0/08/Venus_from_Mariner_10.jpg/440px-Venus_from_Mariner_10.jpg`
      },
      {
        planet: 'Earth',
        gforce: 1,
        img: `https://upload.wikimedia.org/wikipedia/commons/thumb/c/cb/The_Blue_Marble_%28remastered%29.jpg/440px-The_Blue_Marble_%28remastered%29.jpg`
      },
      {
        planet: 'Mars',
        gforce: 0.38,
        img: `https://upload.wikimedia.org/wikipedia/commons/thumb/0/02/OSIRIS_Mars_true_color.jpg/440px-OSIRIS_Mars_true_color.jpg`
      },
      {
        planet: 'Jupiter',
        gforce: 2.36,
        img: `https://upload.wikimedia.org/wikipedia/commons/thumb/2/2b/Jupiter_and_its_shrunken_Great_Red_Spot.jpg/440px-Jupiter_and_its_shrunken_Great_Red_Spot.jpg`
      },
      {
        planet: 'Saturn',
        gforce: 0.92,
        img: `https://upload.wikimedia.org/wikipedia/commons/thumb/c/c7/Saturn_during_Equinox.jpg/600px-Saturn_during_Equinox.jpg`
      },
      {
        planet: 'Uranus',
        gforce: 0.89,
        img: `https://upload.wikimedia.org/wikipedia/commons/thumb/4/48/Uranus_as_seen_by_NASA%27s_Voyager_2_%28remastered%29.png/440px-Uranus_as_seen_by_NASA%27s_Voyager_2_%28remastered%29.png`
      },
      {
        planet: 'Neptune',
        gforce: 1.12,
        img: `https://upload.wikimedia.org/wikipedia/commons/thumb/6/63/Neptune_-_Voyager_2_%2829347980845%29_flatten_crop.jpg/440px-Neptune_-_Voyager_2_%2829347980845%29_flatten_crop.jpg`
      }
    ];
    // Reference <form> and all form controls
    const conv = document.forms.converter;
    const IO = conv.elements;
    /*
    Collect all tags with [name='planet'] and convert it into an array...
    ...iterate through array and define a htmlString and interpolate planet
    name and <img> url...
    ...render htmlString into current <output>
    */
    [...IO.planet].forEach((output, index) => {
      const html = `
      <h3>${data[index].planet}</h3>
      <img src='${data[index].img}'>
      <h4></h4>`;
      output.insertAdjacentHTML('beforeend', html)
    });
    
    // Bind the "input" event to <form>
    conv.oninput = IWC;
    
    /*
    Event handler passes Event Object by default
    Reference all form controls
    Reference the tag the user interacted with
    If the user typed into [name='weight']...
    ...if that tag was also #lbs...
    ...#kg value is the value of >origin< times 0.45359237...
    ...otherwise #lbs value is the value of >origin< times 2.20462...
    */
    /*
    Collect all [name='planet'] into an array and iterate with .forEach()...
    ...clear out the last tag of current <output> (<h4>)...
    ...display the calculated totals for lbs. and kg of each planet
    */
    function IWC(e) {
      const IO = this.elements;
      const origin = e.target;
    
      if (origin.name === 'weight') {
        if (origin.id === 'lbs') {
          IO.kg.value = +origin.value * 0.45359237;
        } else {
          IO.lbs.value = +origin.value * 2.20462;
        }
        [...IO.planet].forEach((output, index) => {
          output.lastElementChild.innerHTML = '';
          output.lastElementChild.insertAdjacentHTML('beforeend',
            `lbs. ${Math.round(data[index].gforce * IO.lbs.value)}<br>
         kg ${Math.round(data[index].gforce * IO.kg.value)}`);
        });
      }
    }
    @import url('https://fonts.googleapis.com/css2?family=Oswald:wght@300&family=Raleway:wght@300&display=swap');
    html {
      font: 300 2ch/1 Oswald;
    }
    
    body {
      width: 100%;
      overflow-x: hidden;
      background: #aaa
    }
    
    h1 {
      color: gold
    }
    
    h1,
    h2,
    h3,
    h4 {
      margin: 0;
      font-family: Raleway;
    }
    
    h3,
    h4 {
      position: absolute;
      z-index: 1;
    }
    
    h3 {
      margin: 0 auto;
    }
    
    h4 {
      bottom: 0
    }
    
    form {
      margin: 0 0 0 -15px;
    }
    
    fieldset {
      display: flex;
      flex-flow: row wrap;
      justify-content: center;
      align-items: center;
      max-width: 90%;
      margin: 15px
    }
    
    fieldset+fieldset {
      background: #222
    }
    
    fieldset+fieldset legend {
      color: gold
    }
    
    output {
      position: relative;
      display: inline-flex;
      width: 24%;
      min-height: 8rem;
      margin-top: 15px;
      padding: 2px;
      background: black;
      color: cyan
    }
    
    input {
      font: inherit;
      width: 10rem;
      margin: 10px;
      text-align: center;
    }
    
    img {
      width: 100%;
    }
    <form id='converter'>
      <fieldset>
        <legend>
          <h1>Interplanetary<br>Weight Converter</h1>
        </legend>
        <input id="lbs" name='weight' placeholder=" Weight in lbs." type="number" min='0' max='99999'><label for='lbs'>lbs.</label>
        <input id="kg" name='weight' placeholder="Weight in kg." type="number" min='0' max='99999'><label for='kg'>kg</label>
      </fieldset>
    
      <fieldset>
        <legend>
          <h2>The Solar System</h2>
        </legend>
        <output name='planet'></output>
        <output name='planet'></output>
        <output name='planet'></output>
        <output name='planet'></output>
        <output name='planet'></output>
        <output name='planet'></output>
        <output name='planet'></output>
        <output name='planet'></output>
      </fieldset>
    </form>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search