0

I can't get my DOM element to connect with my javascript and grab a "test" value.

Client side

<h1 id="redeem" value="test">Header 1</h1>
<script src="/javascripts/redeem.js"></script>

Redeem.js

const guest = document.getElementById('redeem').value;
console.log("redeeem.js working");
console.log(guest);

The redeem.js working text comes through but guest renders as undefined.

How do I get test to come through as my variable?

econobro
  • 315
  • 3
  • 17
  • 2
    `h1` elements do not have a `value`. You should use the `innerText` property if you want the output "Header 1". – Robin Zigmond Dec 28 '22 at 14:08
  • _"getElementById returning as undefined"_ - `.getElementById()` will never return `undefined`. It's either a `Element` or `null`. – Andreas Dec 28 '22 at 14:13
  • Hi @RobinZigmond - I've entered text for a `value` so aren't I able to create a "value" this way? – econobro Dec 28 '22 at 14:27
  • @Andreas - it's rendering as `undefined`. I would send a screenshot if I could. – econobro Dec 28 '22 at 14:28
  • 2
    You already received the answer (and explanation) at the top of your question. I was only "nitpicking" that `.getElementById()` is not the problem. It's the `.value` property you try to access on the return value of `.getElementById()` – Andreas Dec 28 '22 at 14:31
  • 1
    @econobro the `value` property is for inputs where the user can enter a value. See the question linked as a duplicate. – Robin Zigmond Dec 28 '22 at 14:37

0 Answers0