Querying the DOM - Element Attributes
Last updated
Last updated
Open up the html page you made earlier. Open it in the browser, and open the browser console. We're going to query the document for elements and change their attributes, then see the changes in real-time.
Our page currently only has two things: a heading and a button. So lets pick one of them. Run the following in the console:
Now, button
has the button element assigned to it. Test it by typing button
and hitting enter in the console; you should see something like below:
To get an element's properties or attributes, we can use dot notation:
The above line returns the value of the onclick
attribute, which is a function. To make things more interesing, let's go to the html file and add an id attribute:
Reload the page, reopen the console and rerun the above lines to select the button (this time try using the id as a selector instead).
Then run:
This should return "hello", since that is the id we assigned the button.
Given that you have a variable element
that has been assigned an element after querying the DOM, you can access any of its attributes using dot notation:
If the element has not been assigned a given attribute it returns an empty string, or if the attribute is invalid it returns undefined
Example: running the following should return ""
since the button has no class:
Some attributes have default values, such as the hidden
attribute which is false
by default:
Just as an element's attributes can be accessed by JS code, they can also be changed by reassigning them values. Let's try this on our button.
Run the following (after querying for the button if needed):
You won't see any visual change on the page (since the class doesn't affect its appearance) but if you navigate to the Browser Inspector and look for the button, you'll see that it has been assigned a class
attribute. You can even remove the attribute if you wish:
To visualise the change in a better way, try these steps:
Go to your CSS file and add some rules for a class "random-class" (or whatever you want to call it)
Refresh the page, reopen the console, and query for the button
Assign the button a class attribute as above, with the class being "random-class" (or whatever you called it)
You'll see the CSS being applied to the button
Reset the class attribute back to blank. You'll see that the styling no longer applies.
To hide the button (or any element really) just set its hidden
attribute to true
:
You'll see the button disappear from the page. To make it reappear, set the hidden
attribute back to false
:
Any element's (valid) attributes can be assigned or reassigned to any (valid) value by accessing them using dot notation after querying for the element. If an invalid attribute or invalid value is assigned, then it is simply ignored by the page.
If there is no match for the element query, null
is returned by the querySelector
and getElementById
functions. this means that trying to get/reassign any attributes will cause an Uncaught TypeError: x is null
.
There are two ways to get the content of an element. One is to use to .innerHTML
property:
And the other is to use the .innerText
property
The difference between the two is that innerHTML
refers to everything inside the element, whereas innerText
refers to just the text inside the element. In our example there is no difference, but let's try editing our html file temporarily:
Now we have a <span>
element inside the button; a <span>
is just an easy way of encapsulating part of a line so specific styling can be added to it.
Now try getting the contents:
You can change the contents as well by reassigning the properties.
Next, we'll look at how you can modify the style of an element using JS.