Reset/Submit button won't work once text area is edited by user?


Reset/Submit button won't work once text area is edited by user?



My form's reset and submit buttons do not work when I edit the output of the form in the textarea. I created a form that outputs custom sentences based on user inputs. If I don't edit the textarea these buttons work fine, resetting or resubmitting onclick. However, if I edit the textarea all functionality seizes. Any ideas? Thanks! <3




<!DOCTYPE html>
<html>
<head>
<title>Experiment</title>

<style type="text/css">
table,td,th{
margin-left: auto;margin-right: auto
}
.display{
display: flex;align-items: center;justify-content: center;
}
p{
text-align: center;
}
textarea{
display: block;margin-left:auto;margin-right: auto;
}
</style>



function sentence(){
document.getElementById("s1").style.display= 'block';
document.getElementById("r1").style.display= 'block';
{
if(document.getElementById("z1").value==""){
alert("Hi! Your dog's name is required for submission.")
document.getElementById("z1").focus();
}else if(document.getElementById("z2").value==""){
alert("Hi! Your dog's gender is required for submission.")
}else if(document.getElementById("z3").value==""){
alert("Hi! Your dog's size is required for submission.")
}else{
const input1 = document.getElementById("z1").value
const input2 = document.getElementById("z2").value
const input3 = document.getElementById("z3").value
document.getElementById("s1").innerHTML="My dog's name is " + input1 + ". " + input2 + " loves to play fetch. My dog is " + input3 + ".";
}

}
}

function reset(){
document.getElementById("s1").innerHTML="";
}

function hide(){
document.getElementById("s1").style.display= 'none';
document.getElementById("r1").style.display= 'none';
}


</head>
<body onload="hide()">

<table>
<tr>
<td> <input type="text" id="z1" placeholder="Your Dog's Name" name="name" maxlength="100"></td>
<td> <select name="gender" id="z2"> <option value="" disabled selected>Select your dog's gender.</option> <option value="He">He</option> <option value="She">She</option> </select></td>
<td> <select name="size" id="z3"> <option value="" disabled selected>Select your dog's size.</option> <option value="big">big</option> <option value="medium in size">medium</option> <option value="small in size">small</option> </select></td>
</table>
<br>


Submit

<hr>
<br>
<textarea rows="10" cols="100" id="s1"></textarea>
<br>


Reset


</body>
</html>





I indented your code up there... But did not correct obvious syntax errors. A good indentation helps to find such error like an opening bracket { instead of a closing } And missing HTML tags... Good luck buddy... To look for a code editor is my advise.
– Louys Patrice Bessette
Jun 30 at 3:49



{


}




1 Answer
1



The right way to read/write to a textarea is to use value attribute, not innerHTML. Read HTMLTextAreaElement Reference



The code below works,




function sentence() {
document.getElementById("s1").value = "";// reset
document.getElementById("s1").style.display = "block";
document.getElementById("r1").style.display = "block";
if (document.getElementById("z1").value == "") {
alert("Hi! Your dog's name is required for submission.");
document.getElementById("z1").focus();
} else if (document.getElementById("z2").value == "") {
alert("Hi! Your dog's gender is required for submission.");
} else if (document.getElementById("z3").value == "") {
alert("Hi! Your dog's size is required for submission.");
} else {
const input1 = document.getElementById("z1").value;
const input2 = document.getElementById("z2").value;
const input3 = document.getElementById("z3").value;
document.getElementById("s1").value =
"My dog's name is " +
input1 +
". " +
input2 +
" loves to play fetch. My dog is " +
input3 +
".";

}
}

function reset() {
document.getElementById("s1").value = "";
}

function hide() {
document.getElementById("s1").style.display = "none";
document.getElementById("r1").style.display = "none";
}


table,td,th {margin-left: auto;margin-right: auto}
.display {display: flex;align-items: center;justify-content: center;}
p {text-align: center;}
textarea {display: block;margin-left:auto;margin-right: auto;}


<body onload="hide()">
<table>
<tr>
<td> <input type="text" id="z1" placeholder="Your Dog's Name" name="name" maxlength="100"></td>
<td> <select name="gender" id="z2"> <option value="" disabled selected>Select your dog's gender.</option> <option value="He">He</option> <option value="She">She</option> </select></td>
<td> <select name="size" id="z3"> <option value="" disabled selected>Select your dog's size.</option> <option value="big">big</option> <option value="medium in size">medium</option> <option value="small in size">small</option> </select></td>
</table>
<br>


Submit

<hr>
<br>
<textarea rows="10" cols="100" id="s1">

</textarea>
<br>


Reset


</body>






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV