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

10
  • You have no id="select_row" in the HTML, what is let select_row = document.getElementById('select_row'); supposed to get? Commented May 13, 2021 at 1:35
  • i was trying some methods if it works. Can you correct me where i went wrong Commented May 13, 2021 at 1:36
  • What are you trying to save and restore? There are 8 menus in each row. Commented May 13, 2021 at 1: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 Commented May 13, 2021 at 1: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 Commented May 13, 2021 at 1:44

2 Answers 2

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]);
  }
});

Sign up to request clarification or add additional context in comments.

9 Comments

that answer doesnt save to local storage and when the user selects an option it goes back to default web page
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.
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: drive.google.com/file/d/1RHZR7ylpvjWMkEEbvGlTQDEeORJrzNvX/…
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
drive.google.com/file/d/1RHZR7ylpvjWMkEEbvGlTQDEeORJrzNvX/…
|
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

4 Comments

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".
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.
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}...]
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.