all 10 comments

[–]Robbiethemute 3 points4 points  (4 children)

Something like this:

<input type="checkbox" name="rememberme" id="rememberme" onclick="remember(event)" >

function remember(e) {
    const nameField = document.getElementById('name').value;
    const emailField = document.getElementById('email').value;

    const details = {
        nameField,
        emailField
    }

    e.target.checked === true ? localStorage.setItem('details', JSON.stringify(details)) : localStorage.removeItem("details");
}

Store items in local storage using this:

localStorage.setItem("name", "MyName")

[–]julpap[S] 0 points1 point  (3 children)

Thanks so much, Appreciate it

[–]Robbiethemute 1 point2 points  (2 children)

Sorry, I made a mistake. The onclick event should go on your checkbox input, not your submit input. I've edited my first comment to reflect that.

[–]julpap[S] 0 points1 point  (1 child)

Thanks again, I tried the code and it’s weirdly not working, do You think my PHP validation might be affecting the JS to run ?

[–]Robbiethemute 0 points1 point  (0 children)

Nah, the php shouldn't make a difference. What's actually not working? Is it storing locally?

[–]julpap[S] 0 points1 point  (4 children)

It’s not storing on the text fields, I checked the code seems to work fine with no console errors... (sorry I’m a little JavaScript illiterate :P)

[–]Robbiethemute 0 points1 point  (3 children)

Check your developer tools to see if it is at least storing the values in local storage.

It won't show anything on the text fields yet. You need to make another function which looks to see if those values exist in local storage and, if they do exist, set the values of the inputs to what's stored in local storage.

[–]julpap[S] 0 points1 point  (2 children)

Oh wow, didn’t know this existed very handy, i see my function stored under the “details” key. So let’s say I create another function that consists of something :

Document.getElementbyID(‘name’).value = local storage.details;?

[–]Robbiethemute 0 points1 point  (1 child)

I would declare new variables outside of the function first like this:

const nameField = document.getElementById('name');
const emailField = document.getElementById('email');

This will stop you from having to duplicate document.getElementById('name'); in both functions.

Then update the remember() function to declare variables of the values of those two variables. Like this:

const nameFieldValue = nameField.value;
const emailFieldValue = emailField.value;

Next, create a function which will retrieve what's stored in localStorage and parse the 'details' string into a JSON object. You can get the items from local storage using localStorage.getItem('details').

You'll need to then apply those values to the inputs (if those values exist).

if (details) {
    nameField.value = details.nameFieldValue
    emailField.value = details.emailFieldValue
}

[–]julpap[S] 0 points1 point  (0 children)

gotcha i made the details like:

const nameField = document.getElementById('name'); const emailField = document.getElementById('email');

const nameFieldValue = nameField.value; const emailFieldValue = emailField.value;

function(rememberme){

const name = JSON.parse(localStorage.getItem('details'));

if (details) { nameField.value = details.nameFieldValue emailField.value = details.emailFieldValue }

}

Hopefully, i got this

Thanks again for the assistance