document.addEventListener('DOMContentLoaded', function() { var test = document.getElementById("myText").value; var mapObj = { A: 'Eh' }; test = test.replace(/A/g, function(matched) { return mapObj[matched]; }) var check = document.getElementById("check"); check.addEventListener('click', function() { document.getElementById('demo').innerText = test; }, false); }, false); BODY { width: 520px; min-height: 250px; } <html> <body> <textarea rows="10" cols="50" id="myText"></textarea> <button id="check">translate</button> <textarea rows="10" cols="50" id='demo' readonly><p id='demo'></p></textarea> </body> </html> es parte de mi código, es un traductor
se supone que funciona cuando hago clic en el botón traducir y luego imprimo el resultado en el cuadro del área de texto, pero no es así, ¿qué debo hacer para que funcione? Agradecería mucho la respuesta en cualquier tipo
Su variable de prueba no se actualizó cuando presionó el botón. He cambiado un poco el código para darle una idea de por dónde empezar. Echar un vistazo:
document.addEventListener('DOMContentLoaded', function() { var check = document.getElementById("check"); check.addEventListener('click', function() { var test = document.getElementById("myText").value; var mapObj = { A: 'Eh' }; test = test.replace(/A/g, function(matched) { return mapObj[matched]; }) document.getElementById('demo').innerText = test; }, false); }, false); BODY { width: 520px; min-height: 250px; } <html> <body> <textarea rows="10" cols="50" id="myText">Type "A" and press translate.</textarea> <button id="check">translate</button> <textarea rows="10" cols="50" id='demo' readonly></textarea> </body> </html>