<div id="hour" style="display: none">2</div>
JavaScript code:
<script type="text/javascript">
var _h = document.getElementById('hour').value
alert(_h);
</script>
Chrome returns undefined
. What is the problem?
<div id="hour" style="display: none">2</div>
JavaScript code:
<script type="text/javascript">
var _h = document.getElementById('hour').value
alert(_h);
</script>
Chrome returns undefined
. What is the problem?
The .value
property applies to form elements (inputs), not divs. The simplest way to get the contents of your div element is with .innerHTML
:
document.getElementById('hour').innerHTML;
document.getElementById("hour").innerText
or
document.getElementById("hour").innerHTML
divs do not have a value. It is not an input.
You want to use innerHTML or innerText/textContent.
Use
document.getElementById() method for displaying text on google console.
As the other OP said, the div don't have the .value property.
You can see all HTML DOM property at this link: https://www.w3schools.com/JSREF/DEFAULT.ASP
<input id="hour" style="display: none" value="0" >
That should remove the error.