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

277
Views
How to declare a value selected in HTML as a variable?

I have the following code:

<span id="text">Change</span>
<P>
<select class="selector">
    <option value=X1a selected>X1a</option>
    <option value=X2a>X2a</option>
    <option value=X3a>X3a</option>
    <option value=X4a>X4a</option>
</select>
<P>
<span id="WW"></span>
<script>
    let X1a = '"Hello"'
    let X2a = '"Watcha"'
    let X3a = '"Hiya"'
    let X4a = '"YouHoo"'
    let ZZ = text
    document.getElementById("WW").innerHTML = ZZ;
</script>

As you can see, I am selecting a value with id=text. I now want the value in "text" to be a variable in the document.getElementById line to print out the appropriate greeting. But it outputs [object HTMLSpanElement]. How do I make the value which I select a variable for the purposes of the document.getElementById line?

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

0

I think you’re looking for the properties .textContent or .innerHTML. Here’s the differences: https://www.geeksforgeeks.org/difference-between-textcontent-and-innerhtml/. They both allow you to get the value inside a HTML tag. Here’s an example with your code:

let ZZ = document.getElementById("text")
document.getElementById("WW").innerHTML = ZZ.textContent;
about 4 years ago · Juan Pablo Isaza Report

0

Answer: ZZ.textContent - MDN Node.textContent

However, I'd personally store a reference to the element using getElementById just to make it clear what I'm doing, like so:

let textElement = document.getElementById('text');
document.getElementById("WW").innerHTML = textElement.textContent
about 4 years ago · Juan Pablo Isaza Report

0

I don't understand your problem very well. You can do :

let ZZ = document.getElementById("text").textContent
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!