Most elements on a webpage will have some attributes associated with
them. An [a] tag for example will have a src attribute. Other elements may have
attributes such as id, class, title, or style.
Once the browser parses your HTML code, a corresponding DOM node will be created. This node is an
object, and therefore it has properties. In JavaScript we can change these properties, which
are attributes for an HTML element. In other words, attributes refer to an HTML element, and
properties refer to the Node Object of an HTML element.
For instance, this HTML element:
-
< input type="text" value="Name:">
has two attributes (type and value>). Once the browser parses this code, an HTML
Object will be created, and this object will contain dozens of properties. With JavaScript you can
target these properties.
Selecting Elements
We know we can use getElement() and querySelector() to return an element, or
querySelectorAll() to return multiple elements. For example, if we call
document.querySelector('p'); on this webpage, it would return:
because querySelector() will return the 1st instance of the element ([p] in this
example) and "Colt Steele" is the 1st [p] on this page.
However, if we call querySelectorAll('p'); on this page, it would return:
- NodeList(11) [p, p, p, p, p, p, p, p, p, p, p]
because there are eleven [p] tags on this page. We can also expand the node list by clicking the
down arrow (in the console) and it will list all of the [p] tags. We can then further expand each
individual [p] tag to see it's own Object properties.
We can also call querySelectorAll('p')[x]; to list a specific element. if we call
querySelectorAll('p')[5]; we will target the 4th [p] tag (because it starts
counting at 0) which you can then expand to see all of the individual properties for that
[p] object.
getAttribute()
getAttribute() is another way of selecting an element. getAttribute() returns the value of a
specified attribute of an element. If the given attribute does not exist, the value returned will
either be null or "" (an empty string).
Example: if we have the following HTML code,
-
< div id="div1">Hi Champ!< /div>
in the console, if we call:
- const divAttr = document.getElementById("div1");
- console.log(divAttr);
we get:
- < div id="div1">Hi Champ!< /div>
because it returns the entire element, of the only element that has an id = div1.
and then if we console.log(divAttr.getAttribute("id")); we get:
because getAttribute() is returning the value of the attribute "id" of the variable divAttr.
and finally, if we console.log(divAttr.getAttribute("align")); this will return
because there is no attribute align associated with the divAttr variable, which represents the
[div] element.
*note: You can also list a property's current value by console logging .(period)(property). For example:
- console.log(document.querySelectorAll('img')[2].src);
would display what ever the current source property is, for this specific image.
The difference between using an object property directly and using getAttribute()
- using an object property directly would be:
- document.querySelector('a').href;
- this is a JavaScript reference to the href property and would return
- file://linkToFile (or)
- https://linkToFile
- whereas using getAttribute() would be:
- document.querySelector('a').getAttribute('href');
- this is a reference to the href value and would return
- linkToFile (the text of the link path only)
Manipulating Properties
Now comes the interesting part. So we can use:
- getElementById('#id name');
- getElementsByTagName('HTML tag')[specific HTML tag item];
- getElementsByClassName('.class name')[specific class item];
- querySelectorAll('HTML tag')[specific HTML tag];
- getAttribute(); // could be an HTML tag, .class, or #id
to target an HTML Element and retrieve it's DOM Object properties, but how to we actually change or manipulate the attribute/property values?
For which ever method we use to target an HTML element, we can look in the console to see ALL of it's DOM Object properties, such as class, id, innerText, src, and many others. We can then target any one of these properties, by referencing it's label in the DOM properties and attaching it to the end of a selection call (preceded by a . period).
For example, if we have a webpage with some [img] tags, we could target a single tag like so:
- document.querySelector('img')[2];
which would return the DOM Object properties for that specific [img] tag. We could then look at these properties in the console and see the current settings. For example if we wanted to change the source for the image, all we would have to do is:
- querySelectorAll('img')[2].src = 'new image source';
setAttribute()
setAttribute() is another way of changing an existing HTML element's DOM Object properties. So we could modify the src for our image tag, with setAttribute(), like so:
- document.querySelectorAll('img')[2].setAttribute('src', 'new image source');