We help IT Professionals succeed at work.

cookies & HTML

101 Views
Last Modified: 2020-01-08
I am working on a html table and I want to set cookies from the table - that bit I think it okay but what I need to do then is to read that cookie ( from one big long cookie string) and break it down and repost it back to the table when I click my get cookie button.

 I am lost in how to get that bit to work -
EE-First-Sample-Code.txt
Comment
Watch Question

Author

Commented:
I got a little further since but not there yet either …This is my code so far - my set cookie part is working - what is not working is trying to clear the table and then repopulate from the cookie - i.e. the get cookie part ….
Experts-Exchange-Code.txt
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
why not just use local storage

localStorage.setItem and localStorage.getItem
  <button id="save" class="btn btn-primary">Save</button>
  <button id="restore" class="btn btn-primary" style="display: none">Restore</button>
  <table class="table" id="theTable">
    <tr>
      <td>One</td>
      <td>Two</td>
      <td>Three</td>
    </tr>
    <tr>
      <td>One</td>
      <td>Two</td>
      <td>Three</td>
    </tr>
    <tr>
      <td>One</td>
      <td>Two</td>
      <td>Three</td>
    </tr>
    <tr>
      <td>One</td>
      <td>Two</td>
      <td>Three</td>
    </tr>
    <tr>
      <td>One</td>
      <td>Two</td>
      <td>Three</td>
    </tr>
  </table>

Open in new window

jQuery
<script>
$(function() {
  $('#save').click(function() {
    localStorage.setItem("theTable", $('#theTable').html());
    $('#theTable').html('')
    $('button').toggle();
  });
  $('#restore').click(function() {
    var html = localStorage.getItem("theTable");
    $('#theTable').html(html)
    $('button').toggle();
  });
});
</script>

Open in new window

Working sample here

Author

Commented:
Thank you,
The table is dynamic so can’t set the exact table - rows and columns can be added.Also we cannot use jQuery - life would be so much easier if we could
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thank you,
 but that answers it to a specific table - I know what you are saying but I need nested loops to fill my 2d array type table , this doesn’t work - Honestly I have given up on it now - Thanks a mill for posting anyway
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
but I need nested loops to fill my 2d array type table
Can you explain this a bit more - what are you trying to achieve with this?

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.