/
How do I get a DOM element into a Javascript variable?

How do I get a DOM element into a Javascript variable?

One useful method is to use document.querySelector.

This takes a string argument which will accept the same format used by CSS Selectors and return a DOM element if one matches the selector.

Here are a couple of examples:

var Body = document.querySelector("body"); // get the body element var Div = document.querySelector(".APPdiv"); // get an element with the class APPdiv

 

Related content

How do I change the content of a DOM element?
How do I change the content of a DOM element?
More like this
DOM Element
DOM Element
More like this
DOM Elements versus HTML markup
DOM Elements versus HTML markup
More like this
Javascript Object
Javascript Object
Read with this
What is a DOM element under the hood?
What is a DOM element under the hood?
More like this
How can I reset the browser through Javascript?
How can I reset the browser through Javascript?
Read with this