'trying to display value returns undefinded

HTML:

<span class="rbx-text-navbar-right text-header" id="nav-robux-amount">11</span>

CODE:

element = document.getElementById("nav-robux-amount")

if (element){
    window.alert(element.value)
}

window.alert shows that the value of element is undefined



Solution 1:[1]

The .value property only exists on inputs. The 'element' isn't a constant variable.

If you want to get the content, you can do the following:

const element = document.getElementById("nav-robux-amount");

if (element) window.alert(element.textContent)

Note: innerHTML is not as good as textContent, as it is vulnerable to attacks.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Exortions