Basically I’m just trying to clear a text field, which has never been a problem for me.
However, this is NOT working:
<html>
<body>
<form name="form">
<input type="text" name="input-field" value="Value" />
<input type="button" name="clear" value="Clear field" onclick="document.form.input-field.value = ''"; />
</form>
</body>
</html>
But if I rename “input-field” to “inputfield” and onclick to onclick=”document.form.inputfield.value = ””; … Then it works fine.
However… Removing the “-” from the field name, is not an option in my case (long explanation).
So, how do I clear a text field, containing a “-“???
Because it’s not a valid JavaScript identifier. However, you can use the following instead:
It’s for the same reason you can’t declare a variable using
var my-var. On the other hand, if you have anobject, such asformwith several keys, you can access its properties as if it was an associative array:References: