14
<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?

Mike G
  • 4,232
  • 9
  • 40
  • 66
run
  • 543
  • 4
  • 8
  • 20
  • 1
    "In Chrome"? Presumably also a problem in other browsers, or are you saying Chrome is the only one with a problem? – nnnnnn May 22 '12 at 01:33

5 Answers5

46

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;
nnnnnn
  • 147,572
  • 30
  • 200
  • 241
6
document.getElementById("hour").innerText

or

document.getElementById("hour").innerHTML
oezi
  • 51,017
  • 10
  • 98
  • 115
neohope
  • 1,822
  • 15
  • 29
4

divs do not have a value. It is not an input.

You want to use innerHTML or innerText/textContent.

epascarello
  • 204,599
  • 20
  • 195
  • 236
-1

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

-1
<input id="hour" style="display: none" value="0" >

That should remove the error.

Joey
  • 1
  • 1
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34497424) – Wahlstrommm Jun 07 '23 at 11:41