1

I have a HTML table and with question and 8 SELECT ROW OR SELECT from user that requires user to select data and as the user selects on the selection it should save so when the the user refresh/reloads or closes the web-page and opens the web-page back up the data must show on what he selected previously.

I tried the simple method using local storage to get data selected by user to save and display for the first select_row but doesn't work because it get into conflict when i try to get and save the data from same place. so just to see what i did on my work i made the line of code to show under scripts if when the user selects

let doc, htm, bod, nav, M, I, S, Q, hC, aC, rC, tC; // for use on other loads
addEventListener('load', () => {
  doc = document;
  htm = doc.documentElement;
  bod = doc.body;
  nav = navigator;
  M = tag => doc.createElement(tag);
  I = id => doc.getElementById(id);

  mobile = /Mobi/i.test(nav.userAgent);
  S = (selector, within) => {
    let w = within || doc;
    return w.querySelector(selector);
  }
  Q = (selector, within) => {
    let w = within || doc;
    return w.querySelectorAll(selector);
  }
  hC = (node, className) => {
    return node.classList.contains(className);
  }
  aC = (node, ...classNames) => {
    node.classList.add(...classNames);
    return aC;
  }
  rC = (node, ...classNames) => {
    node.classList.remove(...classNames);
    return rC;
  }
  tC = (node, className) => {
    node.classList.toggle(className);
    return tC;
  }
  // small Library above - magic below can be put on another page using a load Event *(except // end load line)*
  const trs = Q('tbody>tr'),
    trsL = trs.length,
    prs = Q('tfoot>tr>td+td'),
    prsM = prs.length - 1,
    ya = [];

  function showAverages() {
    let total = 0;
    for (let p = 0, c, t, l = ya.length; p < prsM; p++) {
      c = 0;
      for (let i = 0; i < l; i++) {
        c += ya[i][p];
      }
      t = (c / l * 100).toFixed(2);
      prs[p].textContent = t;
      total += (+t);
    }
    $("#element").html((total / prsM).toFixed(2) + "%");
  }

  for (let i = 0, r, sels, y; i < trsL; i++) {
    r = trs[i];
    sels = Q('select', r);
    ya.push([]);
    for (let n = 0, s, q = sels.length; n < q; n++) {
      s = sels[n];
      s.value = 'Yes';
      y = s.value === 'Yes' ? 1 : 0;
      ya[i].push(y);
      s.oninput = () => {
        ya[i][n] = s.value === 'Yes' ? 1 : 0;
        showAverages();
      }
    }
  }
  showAverages();
});

//Local Storage Script
let select_row = document.getElementById('select_row');
select_row.addEventListener('input', function() {
  localStorage.setItem('select_row', select_row.value); // Use .value to get and set text of input but use .textContent to get and set text of other elements (textareas, divs, etc.)
})
// This should run on script load
select_row.value = localStorage.getItem('select_row');
thead *,
tfoot * {
  font: bold 16px Arial, san-serif;
}

tfoot>tr>td:not(:last-child):after {
  content: '%';
  font-weight: bold;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="content-page">
  <div class="content">
    <div class="container-fluid">
      <div class="row">
        <div class="col-12">
          <div class="page-title-box">
            <h4 class="page-title"> WebForms &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
              &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; Total Percent Complete = <span id="element"></span></h4>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="col-12">
          <div class="card">
            <div class="card-body">
              <div class="table-responsive">
                <h5 class="mt-0"> WebForm Tasks <br></h5>
                <table id="datatable-buttons" class="table table-striped dt-responsive nowrap w-100" name="table">
                  <thead>
                    <tr>
                      <th class="th-sm">Tasks</th>
                      <th class="th-sm">Avi - Lead</th>
                      <th class="th-sm">Benito</th>
                      <th class="th-sm">Carlos</th>
                      <th class="th-sm">Greg</th>
                      <th class="th-sm">Krish</th>
                      <th class="th-sm">Roy</th>
                      <th class="th-sm">Dee</th>
                      <th class="th-sm">Notes</th>
                    </tr>
                  </thead>
                  <tbody>
                    <tr class="select_row">
                      <td>CR Service Request Form</td>
                      <td>
                        <select>
                          <option value="Yes">Yes</option>
                          <option value="No">No</option>
                        </select>
                      </td>
                      <td>
                        <select>
                          <option value="Yes">Yes</option>
                          <option value="No">No</option>
                        </select>
                      </td>
                      <td>
                        <select>
                          <option value="Yes">Yes</option>
                          <option value="No">No</option>
                        </select>
                      </td>
                      <td>
                        <select>
                          <option value="Yes">Yes</option>
                          <option value="No">No</option>
                        </select>
                      </td>
                      <td>
                        <select>
                          <option value="Yes">Yes</option>
                          <option value="No">No</option>
                        </select>
                      </td>
                      <td>
                        <select>
                          <option value="Yes">Yes</option>
                          <option value="No">No</option>
                        </select>
                      </td>
                      <td>
                        <select>
                          <option value="Yes">Yes</option>
                          <option value="No">No</option>
                        </select>
                      </td>
                      <td> </td>
                    </tr>
                    <tr class="select_row">
                      <td>CR Pallet Reuqest Form</td>
                      <td>
                        <select>
                          <option value="Yes">Yes</option>
                          <option value="No">No</option>
                        </select>
                      </td>
                      <td>
                        <select>
                          <option value="Yes">Yes</option>
                          <option value="No">No</option>
                        </select>
                      </td>
                      <td>
                        <select>
                          <option value="Yes">Yes</option>
                          <option value="No">No</option>
                        </select>
                      </td>
                      <td>
                        <select>
                          <option value="Yes">Yes</option>
                          <option value="No">No</option>
                        </select>
                      </td>
                      <td>
                        <select>
                          <option value="Yes">Yes</option>
                          <option value="No">No</option>
                        </select>
                      </td>
                      <td>
                        <select>
                          <option value="Yes">Yes</option>
                          <option value="No">No</option>
                        </select>
                      </td>
                      <td>
                        <select>
                          <option value="Yes">Yes</option>
                          <option value="No">No</option>
                        </select>
                      </td>
                      <td> </td>
                    </tr>
                  </tbody>
                  <tfoot>
                    <tr class="percent_row">
                      <td>Percent Complete </td>
                      <td>
                      </td>
                      <td>
                      </td>
                      <td>
                      </td>
                      <td>
                      </td>
                      <td>
                      </td>
                      <td>
                      </td>
                      <td>
                      </td>
                      <td>
                      </td>
                    </tr>
                  </tfoot>
                </table>
              </div>
              <!-- end .table-responsive-->
            </div>
            <!-- end card-body -->
          </div>
          <!-- end card -->
        </div>
        <!-- end col -->
      </div>
      <!-- end row -->
    </div>
    <!-- container -->
  </div>
</div>

any data it will save and show but not working. Any help will appreciated

camille
  • 16,432
  • 18
  • 38
  • 60
CruzzerK97
  • 99
  • 9
  • You have no `id="select_row"` in the HTML, what is `let select_row = document.getElementById('select_row');` supposed to get? – Barmar May 13 '21 at 01:35
  • i was trying some methods if it works. Can you correct me where i went wrong – CruzzerK97 May 13 '21 at 01:36
  • What are you trying to save and restore? There are 8 menus in each row. – Barmar May 13 '21 at 01:39
  • I trying to save the response from the user when he selects or choose yes or no from a question. and if he refreshs or reloads or closes the web page and opens back up the web page must display what the user selected last from question – CruzzerK97 May 13 '21 at 01:42
  • for the first question if the user selects yes for avi and no for benito it was save in local storage and after refresh or web pages opens back up the user must see what he selected for each member – CruzzerK97 May 13 '21 at 01:44
  • Just the last question they answered, or all the answers they've selected? – Barmar May 13 '21 at 01:44
  • all the answers they've selected – CruzzerK97 May 13 '21 at 01:46
  • Then you need to put all the selections in an array, save the array as JSON to localstorage. Then when the page reloads, get the array from localstorage and set the values of all the selects. – Barmar May 13 '21 at 01:46
  • im not sure how to execute, can you help me – CruzzerK97 May 13 '21 at 01:47
  • This may help: https://stackoverflow.com/a/3357615/1533592 – dale landry May 13 '21 at 01:52

2 Answers2

2

When the user changes a select, put the values of all the selects in an array, and save it to localStorage as JSON.

When the page loads, parse the JSON, and then update the values of all the selects from the array.

function save_selects() {
  const select_values = Array.from(selects).map(s => s.value);
  localStorage.setItem('select_values', JSON.stringify(select_values));
}

window.addEventListener("load", function() {
  const selects = document.querySelectorAll(".select_row select");
  selects.forEach(s => s.addEventListener('change', save_selects));

  const loc = localStorage.getItem('select_values');
  if (loc) {
    const select_values = JSON.parse(loc);
    selects.forEach((s, i) => s.value = select_values[i]);
  }
});
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • that answer doesnt save to local storage and when the user selects an option it goes back to default web page – CruzzerK97 May 13 '21 at 20:58
  • I've moved the code that adds the event listeners into the `load` event listener, it might have been running before they were loaded into the DOM. – Barmar May 13 '21 at 23:06
  • So i use the code you gave me but i was playing to see to make it work when i go to question and select an option and then click away and then refresh the page it resets back to default. for example i choose in question Yes yes yes and question 2 no yes yess and click anywhere and then click in refresh it submits the form to default or resets and doesnt show any option i choose. Here is whole code i share: https://drive.google.com/file/d/1RHZR7ylpvjWMkEEbvGlTQDEeORJrzNvX/view?usp=sharing – CruzzerK97 May 14 '21 at 01:57
  • When I run and suppose select an option NO and then click somewhere thinking it will save in local storage and when I click refresh or reload the page in 10 to 15 sec it reset to default when I selected NO after refresh it goes to YES – CruzzerK97 May 16 '21 at 21:11
  • drive.google.com/file/d/1RHZR7ylpvjWMkEEbvGlTQDEeORJrzNvX/… – CruzzerK97 May 17 '21 at 18:19
  • Basically im looking to save the yes and no response from user, the percent complete and the total percent complete in local storage – CruzzerK97 May 17 '21 at 19:53
  • I know what you're looking for. I thought my answer does that. You'll have to do your own debugging to figure out why it doesn't work. – Barmar May 17 '21 at 19:54
  • Barmar i search for why the local storage you gave but every method or I even change the names but still it doesn't save in local storage and when refresh it resets to how page was. Here the lastest code I have tried . Here is the link: https://drive.google.com/file/d/1RHZR7ylpvjWMkEEbvGlTQDEeORJrzNvX/view – CruzzerK97 May 19 '21 at 17:47
  • – CruzzerK97 May 19 '21 at 18:50
0

What you could do to reliably get the store and get the data of your select is to store each select value. I made a class per select and used their class name as key to store in the local storage. Then you can modify your existing function to detect if the target has a tag select (or add another class to detect).

So here's the code that I added:

<tr class="select_row">
<td>CR Service Request Form</td>
<td>
  <select class="1">
    <option value="Yes">Yes</option>
    <option value="No">No</option>
  </select>
</td>
<td>
  <select class="2">
    <option value="Yes">Yes</option>
    <option value="No">No</option>
  </select>
</td>
<td>
  <select class="3">
    <option value="Yes">Yes</option>
    <option value="No">No</option>
  </select>
</td>
<td>
  <select class="4">
    <option value="Yes">Yes</option>
    <option value="No">No</option>
  </select>
</td>
<td>
  <select class="5">
    <option value="Yes">Yes</option>
    <option value="No">No</option>
  </select>
</td>
<td>
  <select class="6">
    <option value="Yes">Yes</option>
    <option value="No">No</option>
  </select>
</td>
<td>
  <select class="7">
    <option value="Yes">Yes</option>
    <option value="No">No</option>
  </select>
</td>
<td> </td>

and:

for (let i = 0; i < select_rows.length; i++) {

select_rows[i].addEventListener('click', function (e){ const tgt = e.target;

  if (tgt.tagName == "SELECT") { 
    localStorage.setItem(tgt.className, tgt.value);
  }
})

} localStorage.setItem(tgt.className, tgt.value); } }) }

let doc, htm, bod, nav, M, I, S, Q, hC, aC, rC, tC; // for use on other loads
addEventListener('load', () => {
  doc = document;
  htm = doc.documentElement;
  bod = doc.body;
  nav = navigator;
  M = tag => doc.createElement(tag);
  I = id => doc.getElementById(id);

  mobile = /Mobi/i.test(nav.userAgent);
  S = (selector, within) => {
    let w = within || doc;
    return w.querySelector(selector);
  }
  Q = (selector, within) => {
    let w = within || doc;
    return w.querySelectorAll(selector);
  }
  hC = (node, className) => {
    return node.classList.contains(className);
  }
  aC = (node, ...classNames) => {
    node.classList.add(...classNames);
    return aC;
  }
  rC = (node, ...classNames) => {
    node.classList.remove(...classNames);
    return rC;
  }
  tC = (node, className) => {
    node.classList.toggle(className);
    return tC;
  }
  // small Library above - magic below can be put on another page using a load Event *(except // end load line)*
  const trs = Q('tbody>tr'),
    trsL = trs.length,
    prs = Q('tfoot>tr>td+td'),
    prsM = prs.length - 1,
    ya = [];

  function showAverages() {
    let total = 0;
    for (let p = 0, c, t, l = ya.length; p < prsM; p++) {
      c = 0;
      for (let i = 0; i < l; i++) {
        c += ya[i][p];
      }
      t = (c / l * 100).toFixed(2);
      prs[p].textContent = t;
      total += (+t);
    }
    $("#element").html((total / prsM).toFixed(2) + "%");
  }

  for (let i = 0, r, sels, y; i < trsL; i++) {
    r = trs[i];
    sels = Q('select', r);
    ya.push([]);
    for (let n = 0, s, q = sels.length; n < q; n++) {
      s = sels[n];
      s.value = 'Yes';
      y = s.value === 'Yes' ? 1 : 0;
      ya[i].push(y);
      s.oninput = () => {
        ya[i][n] = s.value === 'Yes' ? 1 : 0;
        showAverages();
      }
    }
  }
  showAverages();
});

//Local Storage Script
let select_rows = document.querySelectorAll('.select_row');

for (let i = 0; i < select_rows.length; i++) {
  select_rows[i].addEventListener('click', function(e) {
    const tgt = e.target;

    if (tgt.tagName == "SELECT") {
      console.log(tgt.value);
      localStorage.setItem(tgt.className, tgt.value);
    }
  })
}
select_row.value = localStorage.getItem('select_row');
thead *,
tfoot * {
  font: bold 16px Arial, san-serif;
}

tfoot>tr>td:not(:last-child):after {
  content: '%';
  font-weight: bold;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="content-page">
  <div class="content">
    <div class="container-fluid">
      <div class="row">
        <div class="col-12">
          <div class="page-title-box">
            <h4 class="page-title"> WebForms &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
              &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; Total Percent Complete = <span id="element"></span></h4>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="col-12">
          <div class="card">
            <div class="card-body">
              <div class="table-responsive">
                <h5 class="mt-0"> WebForm Tasks <br></h5>
                <table id="datatable-buttons" class="table table-striped dt-responsive nowrap w-100" name="table">
                  <thead>
                    <tr>
                      <th class="th-sm">Tasks</th>
                      <th class="th-sm">Avi - Lead</th>
                      <th class="th-sm">Benito</th>
                      <th class="th-sm">Carlos</th>
                      <th class="th-sm">Greg</th>
                      <th class="th-sm">Krish</th>
                      <th class="th-sm">Roy</th>
                      <th class="th-sm">Dee</th>
                      <th class="th-sm">Notes</th>
                    </tr>
                  </thead>
                  <tbody>
                    <tr class="select_row">
                      <td>CR Service Request Form</td>
                      <td>
                        <select class="1">
                          <option value="Yes">Yes</option>
                          <option value="No">No</option>
                        </select>
                      </td>
                      <td>
                        <select class="2">
                          <option value="Yes">Yes</option>
                          <option value="No">No</option>
                        </select>
                      </td>
                      <td>
                        <select class="3">
                          <option value="Yes">Yes</option>
                          <option value="No">No</option>
                        </select>
                      </td>
                      <td>
                        <select class="4">
                          <option value="Yes">Yes</option>
                          <option value="No">No</option>
                        </select>
                      </td>
                      <td>
                        <select class="5">
                          <option value="Yes">Yes</option>
                          <option value="No">No</option>
                        </select>
                      </td>
                      <td>
                        <select class="6">
                          <option value="Yes">Yes</option>
                          <option value="No">No</option>
                        </select>
                      </td>
                      <td>
                        <select class="7">
                          <option value="Yes">Yes</option>
                          <option value="No">No</option>
                        </select>
                      </td>
                      <td> </td>
                    </tr>
                    <tr class="select_row">
                      <td>CR Pallet Reuqest Form</td>
                      <td>
                        <select>
                          <option value="Yes">Yes</option>
                          <option value="No">No</option>
                        </select>
                      </td>
                      <td>
                        <select>
                          <option value="Yes">Yes</option>
                          <option value="No">No</option>
                        </select>
                      </td>
                      <td>
                        <select>
                          <option value="Yes">Yes</option>
                          <option value="No">No</option>
                        </select>
                      </td>
                      <td>
                        <select>
                          <option value="Yes">Yes</option>
                          <option value="No">No</option>
                        </select>
                      </td>
                      <td>
                        <select>
                          <option value="Yes">Yes</option>
                          <option value="No">No</option>
                        </select>
                      </td>
                      <td>
                        <select>
                          <option value="Yes">Yes</option>
                          <option value="No">No</option>
                        </select>
                      </td>
                      <td>
                        <select>
                          <option value="Yes">Yes</option>
                          <option value="No">No</option>
                        </select>
                      </td>
                      <td> </td>
                    </tr>
                  </tbody>
                  <tfoot>
                    <tr class="percent_row">
                      <td>Percent Complete </td>
                      <td>
                      </td>
                      <td>
                      </td>
                      <td>
                      </td>
                      <td>
                      </td>
                      <td>
                      </td>
                      <td>
                      </td>
                      <td>
                      </td>
                      <td>
                      </td>
                    </tr>
                  </tfoot>
                </table>
              </div>
              <!-- end .table-responsive-->
            </div>
            <!-- end card-body -->
          </div>
          <!-- end card -->
        </div>
        <!-- end col -->
      </div>
      <!-- end row -->
    </div>
    <!-- container -->
  </div>
</div>

s for your select. And I used that to store the data in your items

Prosy Arceno
  • 2,616
  • 1
  • 8
  • 32
  • So suppose have i have more than two questions wont it conflict with each other. like one question has class="select row" and the other question has class="7". – CruzzerK97 May 14 '21 at 01:43
  • You can use a different `class` as your key in your local. Maybe on your first `tr` of `select`, add another `class` which is `select_row_1` and then retain the other number for the `selects`. Then for your second `tr` do the same only difference is `select_row_2`. – Prosy Arceno May 14 '21 at 02:20
  • Ideally, you would end up with 2 arrays with objects as their elements and each object is like `{1: Yes}` or something like that. Like this `select_row_1 = [{1: Yes}, {2: No}...]` – Prosy Arceno May 14 '21 at 02:24
  • i search for why the local storage you gave but every method or I even change the names but still it doesn't save in local storage and when refresh it resets to how page was. Here the lastest code I have tried . Here is the link: drive.google.com/file/d/1RHZR7ylpvjWMkEEbvGlTQDEeORJrzNvX/view – CruzzerK97 May 19 '21 at 17:58