I’m experiencing some strang behaviour when trying to clear the value of a text input field. Let’s say the user types something into the below input field–which is part of a form. I would like to use javascript to clear the value in this field (I know I can use a reset button to accomplish this, my scenario is simply for examples sake so I can understand what is going on)
<input type="text" value="" name="input1" id="input1">
# This doesn't clear the value if the user has already
# typed something into the field
var input = document.getElementById("input1");
input.setAttribute("value", "");
# This will clear the value in all scenarious
var input = document.getElementById("input1");
input.value == ""
# This will set the value only if the user hasn't
# already typed something into the field.
var input = document.getElementById("input1");
input.setAttribute("value", "A Value");
Can anyone explain this behaviour?
Thanks,
Steven