1

allCookies contents a list of my Browser Cookies. I want to delete Cookies with this function delCookie(), but It just delete the first Cookie, and not the others cookies. And how could i do to update a cookie???

<input type="button" value="DElete" onclick="delCookie()">
    <input type="button" value="Update" onclick="modCookie()">


<select multiple id="allCookies" size="5">
        <!-- Cookies content-->
    </select><br>

 function delCookie() {
    if (confirm('Are u sure?')) {
        var e = document.getElementById("allCookies");
        var strUser = e.options[e.selectedIndex].value;
        document.cookie = encodeURIComponent(strUser) + "=deleted; expires=" + new Date(0).toUTCString();
    }
}

1 Answer 1

1

It just delete the first Cookie, and not the others cookies.

The selectedIndex property of a <select> element only returns the index of the first selected option. To check all of them in a multiple select, you will need to iterate the options collection:

var os = document.getElementById("allCookies").options;
for (var i=0; i<os.length; i++) {
    if (os[i].selected) {
        var strUser = os[i].value;
        …
    }
}

And how could i do to update a cookie???

Just overwrite the cookie, i.e. use the same method as when creating them.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.