By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – Append values in a sizing chart
JavaScript

Javascript – Append values in a sizing chart

Admin
Last updated: 2024/02/10 at 1:45 PM
Admin
Share
7 Min Read
Append values in a sizing chart

Problem:

I created a sizing chart using HTML and CSS. The user can select the units of measurement in the chart and there is a selection for this. I have written a javascript to append the values based on the selected measure. I thought this was fairly simple but I can’t seem to get around this. Can anyone point out the error in my approach? I am attaching part of my code below

Contents
Problem:Solution:
const metricData = [
  ["72 - 80", "80 - 88", "88 - 96", "81 - 89", "89 - 97", "97 - 109", "109 - 121", "121 - 133", "133 - 145"],
["57 - 65", "65 - 73", "73 - 81", "81 - 89", "89 - 97", "97 - 109", "109 - 121", "121 - 133", "133 - 145"],

];

const imperialData = [
  ["10000 - 80", "80 - 88", "88 - 96", "81 - 89", "89 - 97", "97 - 109", "109 - 121", "121 - 133", "133 - 145"],
["200000 - 65", "65 - 73", "73 - 81", "81 - 89", "89 - 97", "97 - 109", "109 - 121", "121 - 133", "133 - 145"],

];

const measurementSelect = document.getElementById('measurement-select');
const sizingTable = document.getElementById('sizing-table');
const tbody = sizingTable.querySelector('tbody');

measurementSelect.addEventListener('change', function() {
  const selectedOption = measurementSelect.value;
  const data = selectedOption === 'METRIC' ? metricData : imperialData;

  // Loop through the table rows and update the cells with new values
  for (let row = 0; row < data.length; row++) {
    const cells = tbody.querySelectorAll(`[data-row="${row + 2}"]`);
    for (let col = 1; col < cells.length; col++) { // Start at 1 to skip the header cell
      cells[col].textContent = data[row][col - 1];
    }
  }
});
table {
  border-collapse: collapse;
  width: 100%;
}

.toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}
<div class="sizing-chart">
  <div class="toggle">
    <span>Sizing Chart</span>
    <div class="selection">
      <select class="bt-outline" id="measurement-select">
        <option value="METRIC">cm</option>
        <option value="IMPERIAL">inch</option>
      </select>
      <i id="arrowtwo" class="at"></i>
    </div>
  </div>

  <table id="sizing-table">
    <thead>
      <tr>
        <th scope="row" data-column="0" data-row="1" class="even">Size</th>
        <td data-column="1" data-row="1" class="odd">XXS</td>
        <td data-column="2" data-row="1" class="even">XS</td>
        <td data-column="3" data-row="1" class="odd">S</td>
        <td data-column="4" data-row="1" class="even">M</td>
        <td data-column="5" data-row="1" class="odd">L</td>
        <td data-column="6" data-row="1" class="even">XL</td>
        <td data-column="7" data-row="1" class="odd">2XL</td>
        <td data-column="8" data-row="1" class="even">3XL</td>
        <td data-column="9" data-row="1" class="odd">4XL</td>
      </tr>
    </thead>

    <tbody>
      <tr>
        <th scope="row" data-column="0" data-row="2" class="even" system="METRIC">Chest (cm)</th>
        <td data-column="1" data-row="2" class="odd" system="METRIC">72 - 80</td>
        <td data-column="2" data-row="2" class="even" system="METRIC">80 - 88</td>
        <td data-column="3" data-row="2" class="odd" system="METRIC">88 - 96</td>
        <td data-column="4" data-row="3" class="even" system="METRIC">81 - 89</td>
        <td data-column="5" data-row="3" class="odd" system="METRIC">89 - 97</td>
        <td data-column="6" data-row="3" class="even" system="METRIC">97 - 109</td>
        <td data-column="7" data-row="3" class="odd" system="METRIC">109 - 121</td>
        <td data-column="8" data-row="3" class="even" system="METRIC">121 - 133</td>
        <td data-column="9" data-row="3" class="odd" system="METRIC">133 - 145</td>
      </tr>
      <tr>
                 <th scope="row" data-column="0" data-row="3" class="even" system="METRIC">Waist (cm)</th>
                 <td data-column="1" data-row="3" class="odd" system="METRIC">57 - 65</td>
                 <td data-column="2" data-row="3" class="even" system="METRIC">65 - 73</td>
                 <td data-column="3" data-row="3" class="odd" system="METRIC">73 - 81</td>
                 <td data-column="4" data-row="3" class="even" system="METRIC">81 - 89</td>
                 <td data-column="5" data-row="3" class="odd" system="METRIC">89 - 97</td>
                 <td data-column="6" data-row="3" class="even" system="METRIC">97 - 109</td>
                 <td data-column="7" data-row="3" class="odd" system="METRIC">109 - 121</td>
                 <td data-column="8" data-row="3" class="even" system="METRIC">121 - 133</td>
                 <td data-column="9" data-row="3" class="odd" system="METRIC">133 - 145</td>
              </tr>

Solution:

Some of the row numbers are incorrect in your HTML. Check row 2. Here is a snippet with that issue fixed.

const metricData = [
  ["72 - 80", "80 - 88", "88 - 96", "81 - 89", "89 - 97", "97 - 109", "109 - 121", "121 - 133", "133 - 145"],
["57 - 65", "65 - 73", "73 - 81", "81 - 89", "89 - 97", "97 - 109", "109 - 121", "121 - 133", "133 - 145"],

];

const imperialData = [
  ["10000 - 80", "80 - 88", "88 - 96", "81 - 89", "89 - 97", "97 - 109", "109 - 121", "121 - 133", "133 - 145"],
["200000 - 65", "65 - 73", "73 - 81", "81 - 89", "89 - 97", "97 - 109", "109 - 121", "121 - 133", "133 - 145"],

];

const measurementSelect = document.getElementById('measurement-select');
const sizingTable = document.getElementById('sizing-table');
const tbody = sizingTable.querySelector('tbody');

measurementSelect.addEventListener('change', function() {
  const selectedOption = measurementSelect.value;
  const data = selectedOption === 'METRIC' ? metricData : imperialData;

  // Loop through the table rows and update the cells with new values
  for (let row = 0; row < data.length; row++) {
    const cells = tbody.querySelectorAll(`[data-row="${row + 2}"]`);
    for (let col = 1; col < cells.length; col++) { // Start at 1 to skip the header cell
      cells[col].textContent = data[row][col - 1];
    }
  }
});
table {
  border-collapse: collapse;
  width: 100%;
}

.toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}
<div class="sizing-chart">
  <div class="toggle">
    <span>Sizing Chart</span>
    <div class="selection">
      <select class="bt-outline" id="measurement-select">
        <option value="METRIC">cm</option>
        <option value="IMPERIAL">inch</option>
      </select>
      <i id="arrowtwo" class="at"></i>
    </div>
  </div>
</div>

<table id="sizing-table">
  <thead>
    <tr>
      <th scope="row" data-column="0" data-row="1" class="even">Size</th>
      <td data-column="1" data-row="1" class="odd">XXS</td>
      <td data-column="2" data-row="1" class="even">XS</td>
      <td data-column="3" data-row="1" class="odd">S</td>
      <td data-column="4" data-row="1" class="even">M</td>
      <td data-column="5" data-row="1" class="odd">L</td>
      <td data-column="6" data-row="1" class="even">XL</td>
      <td data-column="7" data-row="1" class="odd">2XL</td>
      <td data-column="8" data-row="1" class="even">3XL</td>
      <td data-column="9" data-row="1" class="odd">4XL</td>
    </tr>
  </thead>

  <tbody>
    <tr>
      <th scope="row" data-column="0" data-row="2" class="even" system="METRIC">Chest (cm)</th>
      <td data-column="1" data-row="2" class="odd" system="METRIC">72 - 80</td>
      <td data-column="2" data-row="2" class="even" system="METRIC">80 - 88</td>
      <td data-column="3" data-row="2" class="odd" system="METRIC">88 - 96</td>
      <td data-column="4" data-row="2" class="even" system="METRIC">81 - 89</td>
      <td data-column="5" data-row="2" class="odd" system="METRIC">89 - 97</td>
      <td data-column="6" data-row="2" class="even" system="METRIC">97 - 109</td>
      <td data-column="7" data-row="2" class="odd" system="METRIC">109 - 121</td>
      <td data-column="8" data-row="2" class="even" system="METRIC">121 - 133</td>
      <td data-column="9" data-row="2" class="odd" system="METRIC">133 - 145</td>
    </tr>
    <tr>
       <th scope="row" data-column="0" data-row="3" class="even" system="METRIC">Waist (cm)</th>
       <td data-column="1" data-row="3" class="odd" system="METRIC">57 - 65</td>
       <td data-column="2" data-row="3" class="even" system="METRIC">65 - 73</td>
       <td data-column="3" data-row="3" class="odd" system="METRIC">73 - 81</td>
       <td data-column="4" data-row="3" class="even" system="METRIC">81 - 89</td>
       <td data-column="5" data-row="3" class="odd" system="METRIC">89 - 97</td>
       <td data-column="6" data-row="3" class="even" system="METRIC">97 - 109</td>
       <td data-column="7" data-row="3" class="odd" system="METRIC">109 - 121</td>
       <td data-column="8" data-row="3" class="even" system="METRIC">121 - 133</td>
       <td data-column="9" data-row="3" class="odd" system="METRIC">133 - 145</td>
    </tr>
  </tbody>
</table>

Expand snippet

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Issue value of checkbox not affect on model property speakstuff when call action? Javascript – Issue value of checkbox not affect on model property speakstuff when call action?
Next Article How to dynamically create a subclass from a given class and enhance/augment the subclass constructor's prototype? Javascript – How to dynamically create a subclass from a given class and enhance/augment the subclass constructor’s prototype?
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?