Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
187
rated 0 times [  188] [ 1]  / answers: 1 / hits: 56545  / 8 Years ago, fri, september 2, 2016, 12:00:00

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:
https://jsfiddle.net/c3jtg60v/


The HTML:


<p id="theInput">
desired-value
</p>

The Javascript:


var value = document.getElementById("theInput").value;
console.log(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",


More From » html

 Answers
23

.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;

[#60838] Tuesday, August 30, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
peytont

Total Points: 215
Total Questions: 110
Total Answers: 111

Location: Armenia
Member since Sat, Dec 31, 2022
1 Year ago
;