To start with, if you are already using jQuery,
document.getElementById
is not needed, as you can use jQuery
selectors:
http://api.jquery.com/category/selectors/[
^].
Now the solution: text box is actually the
input
element with the attribute
type="text"
. For all input elements, its value is accessed through its property
value
:
http://www.w3schools.com/tags/tag_input.asp[
^],
http://www.w3schools.com/tags/att_input_value.asp[
^],
http://www.w3schools.com/jsref/prop_option_value.asp[
^].
So, apparently, you only need to assign this value to empty string, in the handler of the event you need under condition you need.
However, I would not advise you to do it. Usually, the user knows better what to delete from input data and when. By the user will be only frustrated if the input data is cleaned without her/his order. Maybe the user wants to repeat the same value or modify just one or too characters in it? If I, personally, came across the behavior you describe, I would do all my best to avoid your site. Some of your users might feel the same. Do you need it?
—SA