NoOneSpecial
NoOneSpecial

Reputation: 43

innerHTML and document.getElementbyId().value conflicting?

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

Answers (3)

klawipo
klawipo

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

Fabiano Araujo
Fabiano Araujo

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

gdoron
gdoron

Reputation: 150253

value is for <form> elements like <input>, <progressbar> etc, for <li> you should use innerHTML as you already do.

Upvotes: 1

Related Questions