I want to get a value from a HTML tag using javaScript,
I'm trying to do this
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<span id="year"> 2021 </span>
</body>
<script>
const YEAR = document.getElementById('year');
alert(YEAR);
</script>
</html>
Instead of getting 2021 in alert I get [object HTMLSpanElement]
So how can get 2021 in YEAR variable ??
When using the document
object you have access to hundreds of properties methods.
You can use innerHTML()
to retrieve the html value
with the selected id
attribute.
let year = document.getElementById('year').innerHTML;