HowDoiJava HowDoiJava - 1 year ago 44
HTML Question

How to get value of HTML element in javascript?

so I'm having trouble with getting the value of a specific field in my code - I made this pretty simple example to show my problem, here is the JSFiddle:


<p id="theInput">

The Javascript:

var value = document.getElementById("theInput").value;

As is, I have assigned a p element with an id of "theInput", and in it I have the desired value, that I want to make use of in my javascript.

At the moment, all I'm trying to do is console.log the value, but when I do this, all i get logged in the console is 'undefined'. What I really want to get logged to the console is "desired value",

Any help on this would be greatly appreciated!

EDIT: I like how I get lots of downvotes, and the question people are sending me to, is "Getting innerhtml value into the variable" - The problem was I didn't know about innerHTML, how would I know that question is relevant?? come on people!

Answer Source

.value refers to a value property, which cannot be set for a P tag. If you want the inner text of a html tag, you could do this:

var value = document.getElementById("theInput").innerHTML;