Reputation: 43
I've run into a bit of a problem. I'm using document.getElementById("foo").innerHTML
to set the value of a <li>
. Then, later in the code, I'm accessing it using document.getElementById("foo").value
. However, this returns "undefined". The code looks a bit like this:
document.getElementById("myElement").innerHTML = "This is a test";
...
var v = document.getElementById("myElement").value; // Returns "undefined"
I've tried multiple ways to fix this, to no avail. Does anyone know what's going on?
Upvotes: 0
Views: 1692
Reputation: 153
The element must be an input field to have a value. Li is not an input field, so value is undefined.
Upvotes: 0
Reputation: 932
value
attribute is available for inputs. As you set innerHTML
, you can get it just referencing it as: document.getElementById("myElemento").innerHTML
.
Upvotes: 1
Reputation: 150253
value
is for <form>
elements like <input>
, <progressbar>
etc, for <li>
you should use innerHTML
as you already do.
Upvotes: 1