Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store value in html / jquery

Tags:

html

jquery

i need to store few element in temp variable, so when i get back to that pahe i can show them on page load

i have about 8 li element but let's i click on 5 li, so i want to store 5 li id somewhere

$("#divPopup").on("click", "li", function () {
            ...
                var newId = $this.attr('Id').replace("Left", "Right")

                // here i want to store newID, each time user click on event, it should be unique values  , i want to store in $('#hdnValueProjectBtn').val(newId);
            }
            else {
                var newId = $this.attr('Id').replace("Left", "Right")
                $('#' + newId).hide();
            }
        });
like image 381
Nestor C Avatar asked Dec 13 '25 16:12

Nestor C


1 Answers

You can use localStorage in supported browsers and fallback to Cookies in older ones, here is an example:

// store value
if (window.localStorage !== undefined) {
    window.localStorage.setItem("li_id", id);
} else {
    // WARNING: the following will overwrite current document cookie.
    document.cookie = ["li_id", "=", id, "; domain=.", window.location.host, "; path=" + window.location.pathname + ";"].join("");

}

// get value
var savedValue;
if (window.localStorage !== undefined) {
    savedValue = window.localStorage.getItem("li_id");
} else {
    savedValue = document.cookie.split(";")[0].split("=")[1];
}

alert(savedValue);

Note that id in snippet above is the value you want to store.

like image 58
fardjad Avatar answered Dec 15 '25 09:12

fardjad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!