Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

74
Views
Add a value to a cookie with expiration data?

How can I add a cookie which expires in 2 months to 'test@gmail.com' from the following code

<div id="home">
    <div class="name" my-data="123">Email: test@gmail.com</div> 
</div>

I think the code for the actual time should be as follows but I do not know how to add the value to it:

function myemail () {
    var expiryDate = new Date();
  expiryDate.setMonth(expiryDate.getMonth() + 2);
    document.cookie = cookieName + '=y; expires=' + expiryDate.toGMTString
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Assuming you have privileges to add an ID to your div, the following code works fine

<body>

    <div id="home">
        <div class="name" my-data="123" id="email-div">Email: test@gmail.com</div>
        <button onclick="myemail()">Set it</button>
    </div>

    <script>
        function myemail() {
            var expiryDate = new Date();
            var email = document.getElementById("email-div").textContent.split("Email:")[1]
            expiryDate.setMonth(expiryDate.getMonth() + 2);
            document.cookie = 'emailCookie=' + email + ';expires=' + expiryDate.toGMTString() + ';path=/';
        }
    </script>

</body

Well I have used a button to trigger the action. You can do the same in any event.

about 4 years ago · Juan Pablo Isaza Report

0

toGMTString is a function, you need to call. Replace expiryDate.toGMTString to expiryDate.toGMTString()

const Cookie = {
get: function (name) {
    const nameEq = name + "=";
    const ca = document.cookie.split(';');
    for (let i = 0; i < ca.length; i++) {
        let c = ca[i];

        while (c.charAt(0) == ' ') {
            c = c.substring(1, c.length);
        }

        if (c.indexOf(nameEq) == 0) {
            return c.substring(nameEq.length, c.length);
        }
    }

    return null;
},

set: function (name, value, hours) {
    const expires = "";
    if (hours) {
        let date = new Date();
        date.setTime(date.getTime() + (hours * 60 * 60 * 1000));
        const expires = "; expires=" + date.toGMTString();
    }

    document.cookie = name + "=" + value + expires + "; path=/";
}
};

You can change hours to whatever and of course this one date.setTime(date.getTime() + (hours * 60 * 60 * 1000)); to the corresponding formula

Usage`

Set: Cookie.set('myemail', 'test@gmail.com', 1460) // 1460 is 2 months in hours

Get: Cookie.get('myemail')

const email = document.getElementsByClassName('name')[0].textContent.split(' ')[1]; Cookie.set('myemail', email, 1460);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!