Hola, estoy aprendiendo java script. Actualmente estoy tratando de averiguar la expresión regular. Quiero probar la opción cuando ingreso algo en el campo del área de texto, hacer clic automáticamente en el botón para eliminar el texto ingresado en el campo e imprimir el texto regex que será el predeterminado, y reemplazar las entidades HTML con su visual pantalla: &erio; en &, con un espacio, & quot; con comillas.
<!DOCTYPE html> <html> <body> <textarea id="demo" name="w3review" rows="4" cols="50"> </textarea> <br> <button id="btnText" onclick="tipka()">click</button> <script> function tipka(){ let str = document.getElementById("demo").innerHTML; let res = str.replace(/abc/g, "Lorem Ipsum is simply dummy text of the printing and typesetting industry."); document.getElementById("demo").innerHTML = res;} </script> </body> </html>Si entiendo la pregunta, debería haber document.getElementById("demo").value no document.getElementById("demo").innerHTML
<!DOCTYPE html> <html> <body> <textarea id="demo" name="w3review" rows="4" cols="50"> </textarea> <br> <button id="btnText" onclick="tipka()">click</button> <script> function tipka(){ let str = document.getElementById("demo").value; let res = str.replace(/abc/g, "Lorem Ipsum is simply dummy text of the printing and typesetting industry."); document.getElementById("demo").value= res; } </script> </body> </html>La solución requiere algunos cambios:
tipka , la función debe leer el value del área de textarea en lugar del HTML innerHTML .value del campo de innerHTML textfieldConsulte Configuración de innerHTML frente a configuración de valor con Javascript
Aquí está el fragmento que funciona con los cambios sugeridos:
<!DOCTYPE html> <html> <body> <textarea id="demo" name="w3review" rows="4" cols="50"> </textarea> <br> <button id="btnText" onclick="tipka()">click</button> <script> function tipka() { let str = document.getElementById("demo").value; let res = str.replace(/abc/g, "Lorem Ipsum is simply dummy text of the printing and typesetting industry."); document.getElementById("demo").value = res; } </script> </body> </html>