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

242
Views
¿Hay alguna forma de borrar un área de texto sin usar una función de clic?

Quiero lograr un borrado automático y mostrar otro valor que corresponda a los nuevos datos ingresados

Este es mi código html para datos de entrada y área de texto.

 function getInputValue(){ var integer = document.getElementById("integer").value; var text = document.getElementById("answer"); for (var i = 1; i <= integer; i++) { if (i % 15 == 0){ text.append( i, "= ","fizzbuzz\n") } else if (i % 3 == 0){ text.append( i, "= ","fizz \n") } else if (i % 5 == 0){ text.append( i, "= ","buzz \n") } else{ text.append(i, "= \n") } } }
 <div class="form-floating mb-3"> <input class="form-control" id="integer" onChange="getInputValue()" type="text" placeholder="Input an Integer for N" data-sb-validations="required" /> <label for="integer">Input an Integer for N</label> </div> <div class="col-lg-6 form-floating answer mb-3"> <textarea disabled class="form-control" id="answer" type="text" placeholder="Answer" style="height: 20rem"></textarea> </div>

Si intenta ejecutar el fragmento, puede ver que si cambia el valor en la etiqueta de entrada, el área de texto simplemente apilará todas las salidas.

over 4 years ago · Santiago Trujillo
4 answers
Answer question

0

Por lo que puedo ver aquí, no hay una forma de borrar un área de texto sin usar una función de clic.

over 4 years ago · Santiago Trujillo Report

0

Debido a que está usando append() siempre, siempre está agregando contenido. Simplemente onChange cada vez que se active onChange para que se reinicie. Puedes usar: text.innerHTML = '';

 function getInputValue(){ var integer = document.getElementById("integer").value; var text = document.getElementById("answer"); text.innerHTML = ''; for (var i = 1; i <= integer; i++) { if (i % 15 == 0){ msg = i + "fizzbuzz"; text.append( i, "= ","fizzbuzz\n") } else if (i % 3 == 0){ text.append( i, "= ","fizz \n") } else if (i % 5 == 0){ text.append( i, "= ","buzz \n") } else{ text.append(i, "= \n") } } }
 <div class="form-floating mb-3"> <input class="form-control" id="integer" onChange="getInputValue()" type="text" placeholder="Input an Integer for N" data-sb-validations="required" /> <label for="integer">Input an Integer for N</label> </div> <div class="col-lg-6 form-floating answer mb-3"> <textarea disabled class="form-control" id="answer" type="text" placeholder="Answer" style="height: 20rem"></textarea> </div>

over 4 years ago · Santiago Trujillo Report

0

Intente usar el evento oninput en lugar del evento onchange .

over 4 years ago · Santiago Trujillo Report

0

Aquí está la solución:

 let inputEl = document.querySelector('#integer') let answerEl = document.querySelector('#answer') inputEl.addEventListener('input', () => { answerEl.value = '' if (inputEl.value !== '') { answerEl.value = getInputValue() } }) function getInputValue() { var integer = inputEl.value; var text = ''; for (var i = 1; i <= integer; i++) { if (i % 15 == 0) { text += i +"= fizzbuzz\n" } else if (i % 3 == 0) { text += i +" = fizz \n" } else if (i % 5 == 0) { text += i +" = buzz \n" } else { text += i +" = \n" } } return text }
 <div class="form-floating mb-3"> <input class="form-control" id="integer" type="text" placeholder="Input an Integer for N" data-sb-validations="required" /> <label for="integer">Input an Integer for N</label> </div> <div class="col-lg-6 form-floating answer mb-3"> <textarea disabled class="form-control" id="answer" type="text" placeholder="Answer" style="height: 20rem"></textarea> </div>

Por contexto.

  1. Eliminar el onChange="getInputValue()" en línea
  2. Configurar getInputValue como un tipo de retorno
  3. Compruebe si la entrada está vacía, de lo contrario, ejecute la función
over 4 years ago · Santiago Trujillo 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!