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

177
Views
How can I assign an object's property to a variable ..,?

I have five variables each contain an editable div element .. I want to create an object with five properties then assign these properties to the innerText of the five variables ..

let firstName = document.querySelector(".firstName").innerHTML
let secondName = document.querySelector(".secondName").innerHTML
let marsName = document.querySelector(".marsName").innerHTML
let email = document.querySelector(".phoneOrEmail").innerHTML
let password = document.querySelector(".password").innerHTML
let All_is_well = document.querySelector(".allIsWell");
All_is_well.addEventListener("click", () => {
let form = {
 firstName,
 secondName,
 marsName,
 email,
 password,
}
 fetch(
"http:localhost:5000/api/register",
{
  method: "POST",
  body: JSON.stringify(form),
  headers: { "Content-Type": "application/json" },
  cache: 'no-cache'
}
)
.then(res.json())
.then((data) => {
  // Handle response
  console.log("Success: ", res);
})
.catch((error) => {
  // Handle error
  console.log("Error: ", error);
});

console.log(form);
});
<form>
  <div class="comment"> install your landed spaceship on mars </div>
  <div class="firstName"      contenteditable="true"  spellcheck="false"> </div>
  <div class="secondName"     contenteditable="true"  spellcheck="false"> </div>
  <div class="marsName"       contenteditable="true"  spellcheck="false"> </div>
  <div class="phoneOrEmail"   contenteditable="true"  spellcheck="false"> </div>
  <div class="password"       contenteditable="true"  spellcheck="false"> </div>
  <button type="submit" class="allIsWell"> All is Well </button>

</form>

when I print the object in the console, each property is assigned a value " ".

enter image description here

enter image description here

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Does your divs got the right classes ?

Please make sure you're saving values after editing the divs.

For example, by clicking on a "Save" button.

document.getElementById("save").addEventListener("click", () => {
  let first = document.querySelector(".element1").innerText;
  let second = document.querySelector(".element2").innerText;
  let third = document.querySelector(".element3").innerText;

  let props = {};

  props.first = first;
  props.second = second;
  props.third = third;

  console.log(props); // returns the right values for me
});
about 4 years ago · Santiago Gelvez 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!