Hi I'm learning java script. I am currently trying to figure out the regex. I want to try the option when I enter something in the text area field, to automatically click on the button to delete the text entered in the field and print the regex text some that will be the default, and to replace HTML entities with their visual display: & amp; in &, & nbsp; with a space, & quot; with quotation marks.
<!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>
CodePudding user response:
The solution requires a few changes :
- Inside
tipka
, the function should read thevalue
of thetextarea
instead of theinnerHTML
. - Similarly, while replacing the text, set the
value
of thetextfield
instead ofinnerHTML
.
See Setting innerHTML vs. setting value with Javascript
Here's the snippet working with the suggested changes :
<!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>
CodePudding user response:
If i understand question there should be document.getElementById("demo").value
not 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>