Last Updated : Jan 1, 2023
As, we know that cookies are used to store data inside user’s device or computer.
For example, at time of login we save the username, email id, etc. of user inside cookies.
And at dashboard we use that cookie to show user text like, Welcome username to our portal, etc.
Here, we did not retrieve all the data of cookie we get cookie by using the name in which we stored that cookie to access required data.
document.cookie = "key1 = value1; key2 = value2; expires = date";
Below is the example of get cookies by name for better understanding.
- For this first of all we here create a basic structure of html. For this we use html, head, title and body with script tag. We hope you know about the basic structure of html and which html tag is used for which concept.
- Here, as we see that inside html code, we create a button and paragraph. On click on this button the cookie values that are stored inside cookie name username will displayed on screen.
- Now, as we see inside our script tag, we see a function that calls on button click and this function calls another function as we see. Now, this function gets the cookies related to required name, split them stores the values inside an array. Then the array is used to retrieve required cookies.