Jump to content
Killersites Community

Dynamic HTML Table That Saves Content!

Recommended Posts

Hey guys,  I'm new to programming and I am working on a little project I thought of. I'm trying to create a study logger that you can update and it saves into local storage, so that when you reload the page it saves your old entries and you can view your personal log. So far, I have it working as far as adding/deleting entries.

My issue is that I cannot get the table to save in the browser's local storage, I think because it saves an object. I've read on other posts that I can somehow stringify it, and then parse it? But I've tried that and didn't have any luck. I might have been doing it wrong though. Let me know what you think. I am new to programming so I'm sorry if my code is hard to read! Let me know if you need any clarification. I really appreciate any help. I am open to other ways to tackle this project! I really want to finish this.

Here's my code if you want to take a look:

<form id="tracker" action="" method="post">
  <label>Hours worked:<br></label>
  <input type="number" name="hours" id="hours"><br>
  <input type="date" name="date" id="date"><br><br>
  <label>What did you get done?:<br></label><br>
  <textarea id="accomplished" name="accomplished"></textarea><br>
<button id="submit-btn" onclick="localStore()">Submit!</button>

<-- Table to display logged entries -->
<div class="container">
 <h2>Your Log:</h2>
<table id="myLog" class="table table-bordered">
    <th>What you did</th>
    <th>Hours Studied</th>
    <th>Delete Row</th>




JS and jQuery:
//Stops button from refreshing page
$("#submit-btn").click( function(event) {

//Delete button

var deleteBtn = '<input id="delete-row-btn" type="button" value="X" 

//Delete button code
function deleteRow(btn) {
    var row = btn.parentNode.parentNode;

//Probably a very inefficient way of adding elements to the table!

function localStore() {
    var hours = document.getElementById('hours').value;
    var date = document.getElementById('date').value;
    var accomplished = document.getElementById('accomplished').value;

    localStorage.setItem("hours", hours);
    localStorage.setItem("date", date);
    localStorage.setItem("accomplished", accomplished);

    var table = document.getElementById("myLog");
    var row = table.insertRow(1);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    var cell3 = row.insertCell(2);
    var cell4 = row.insertCell(3);
    cell1.innerHTML = localStorage.date;
    cell2.innerHTML = localStorage.accomplished;
    cell3.innerHTML = localStorage.hours;
    cell4.innerHTML = deleteBtn;

Share this post

Link to post
Share on other sites

I've tested the browser by setting and getting items from local storage between refreshes.  I believe my problem is that I was trying to save the entire table as a local storage property.  From what I've read, you cannot save the entire table as a property because it is not a string, so you have to stringify it, and then parse it. I think that is the part that I am messing up on. Still playing around with it.

Share this post

Link to post
Share on other sites

I haven't used local storage, so I am taking a stab in the dark here. Could you save you information to a JSON structured object and try to save that?


Share this post

Link to post
Share on other sites

Hello domdag27,

Here is a little program that solves your problem. You can test it by using the three files (test.html, screen.css and local-storage2.js) I've attached to my reply.
It can of course be further improved, for example by checking that no two tasks have the same identifier before being added and displayed in your table.

As a general rule of thumb, you should always split up your JavaScript programs into several parts (= or little functions) to write it and improve it more easily. You'll see that in this one, there is seven little functions each dedicated to a specific purpose. 

I've commented out as much as I could this javascript program. Enjoy!





  • Upvote 1

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now