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

76
Views
Assign the same random ID to three different keys

I have an ID generator. I would like the same ID to be assigned to 3 different keys in local-storage. Here is my code:

var ID = function () {
  return "_" + Math.random().toString(36).substr(2, 9);
};

let user_name = document.getElementById("username");
let user_paswrd = document.getElementById("password");
let user_email = document.getElementById("email");

let store_data = () => {
  let i = 0;
  let input_username = localStorage.setItem(
    "username_nr" + ID(),
    user_name.value
  );
  let input_password = localStorage.setItem(
    "password" + ID(),
    user_paswrd.value
  );
  let input_email = localStorage.setItem("email" + ID(), user_email.value);
};

Now the generator assigns 3 different IDs to the keys "username", "password" and "email". How can I change this so that the generator assigns the same ID to the keys?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Rather than calling ID 3 times, each time generating a new ID, call it a single time and use the same value in each place that it's needed:

let store_data = () => {
  let id = ID();
  let i = 0;
  let input_username = localStorage.setItem(
    "username_nr" + id,
    user_name.value
  );
  let input_password = localStorage.setItem(
    "password" + id,
    user_paswrd.value
  );
  let input_email = localStorage.setItem("email" + id, user_email.value);
};
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!