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

268
Views
¿Cómo declarar un valor seleccionado en HTML como una variable?

Tengo el siguiente código:

 <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>

Como puede ver, estoy seleccionando un valor con id=texto. Ahora quiero que el valor en "texto" sea una variable en la línea document.getElementById para imprimir el saludo apropiado. Pero genera [object HTMLSpanElement] . ¿Cómo convierto el valor que selecciono en una variable a los efectos de la línea document.getElementById ?

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

0

Creo que estás buscando las propiedades .textContent o .innerHTML . Aquí están las diferencias: https://www.geeksforgeeks.org/difference- between -textcontent-and-innerhtml/ . Ambos le permiten obtener el valor dentro de una etiqueta HTML. Aquí hay un ejemplo con su código:

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

0

Respuesta: ZZ.textContent - MDN Node.textContent

Sin embargo, personalmente almacenaría una referencia al elemento usando getElementById solo para dejar en claro lo que estoy haciendo, así:

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

0

No entiendo muy bien tu problema. Tu puedes hacer :

 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!