In most cases, yes, but I wouldn't recommend it. Per the specification (and universally supported by browsers), elements in the DOM with id
attributes show up as global variables with the id
's value as their name. So you could use
example.innerHTML
But, the global namespace is crowded and there's a lot going on, with lots of potential for conflicts (for instance, id="name"
doesn't make name
refer to the element). Instead, give yourself a handy, reusable function:
function gid(id) {
return document.getElementById(id);
}
It's also probably worth mentioning that relying on id
s a lot tends to be a bit of an anti-pattern.
.
– Michael T Jan 21 '19 at 12:25