-2

var btn = document.getElementById('insert'),
  s = 0;    //To increment the key(+1) each time the button is clicked and the user inputs the values so the first object's key is 1 then the second one is 2 and so on
btn.addEventListener('click', function() {
      var ModelMake = prompt("Enter The Model Make"),
        ModelYear = prompt("Enter The Model Year"),
        Km = prompt("Enter The Amount Of Km"),
        Price = prompt("Enter The Price"),
        Status = prompt("Enter The Car's Status"),
        table = document.getElementById('table');
      var tableout = document.getElementById('tableout'),
        FinalPrice,
        Details;

      localStorage.setItem(s += 1, JSON.stringify({
        ModelMake: ModelMake,
        ModelYear: ModelYear,
        Km: Km,
        Price: Price,
        Status: Status,
        FinalPrice: FinalPrice,
        Details: Details
      }));

This code inserts the following variables values(user inputs them) in a row and each value is in a column (cell), when i refresh the object IS stored in the localStorage even when i close my browser obviously but i want a way to keep it in the html whether i refresh or close the browser without having to extract it from the localStorage because i don't know how and it's not really what i want.

Murad Elboudy
  • 127
  • 2
  • 2
  • 11
  • I don't understand what you exactly want. You store it in the localStorage, but you don't want to do anything with it? – SBD Dec 18 '14 at 15:29
  • You can't. HTTP request do not remember their previous states, this is why you need some sort of browser or database storage. I recommend you keep using `localStorage`, as it's meant to keep data after refresh and close. As I see it it seems to be a *"I want to get a cookie without opening the cookie jar"*, type of question. – Spencer Wieczorek Dec 18 '14 at 15:29

2 Answers2

0

You could store your data in a cookie. Here is a tutorial that may be of use: Javascript and Cookies

AperioOculus
  • 6,641
  • 4
  • 26
  • 37
0

If you want to do exactly, what you are asking I see few options for you:

  1. on page load you can go thru all localStorage keys, find max number and assign it to S(Here is answer how to go thru all keys: Get HTML5 localStorage keys). I don't really like this way, but...
  2. Store s value as separate key/value pair and extract it on page load
Community
  • 1
  • 1
Uriil
  • 11,948
  • 11
  • 47
  • 68