OnChange textarea detection and clear input field
I am trying to clear an input field on a form when the user changes the content of the textarea using an onchange event handler. I can't see where my error is, why it doesn't work.
Here is my sample code: JSBin
function clear()
{
document.getElementById('clearme').value= " " ;
}
<input type="text" id="clearme" value="123" readonly>
<textarea type="text" id="test2" onChange="clear()">some text here</textarea>
+3
source to share
1 answer
The problem is with the function name clear
. This function already exists in the browser, it is used to clear the console window (it probably should have been called console.clear()
, but this is a top-level function). So yours onchange
is calling this function.
Give your function a different name (I used clearfield()
) and it will work.
function clearfield()
{
document.getElementById('clearme').value= " " ;
}
<input type="text" id="clearme" value="123" readonly>
<textarea type="text" id="test2" onChange="clearfield()">some text here</textarea>
+6
source to share