DOM Flashcards
in css how to target input field but only of type password
input[type=”password”]{
height: 50px;
}
address element by id
document.getElementById(‘id’);
null if not found
can only be called on document
find all fields on the page
var inputs = document.getElementsByTagName(‘input’);
returns a list!!! not an array
but we can address indexes in the same way as with array
inputs[0]
but not pop or push
iterate with for loop is possible
=======
but if we use spread, we can use it as array
const arr = […inputs];
we can call this not only on document, but on other elements too (searching inside their children)
find elements by class
var headers = document.getElementsByClassName(‘header’);
returns a list (HTMLCollection)!!! not an array
but we can address indexes in the same way as with array
inputs[0]
but not pop or push
iterate with for loop is possible
=======
but if we use spread, we can use it as array
const arr = […headers];
we can call this not only on document, but on other elements too (searching inside their children)
find by css identifiers
document. querySelector(‘h1’);
document. querySelector(‘#red’);
document. querySelector(‘section ul li.special’);
document. querySelector(‘input[type=”password”]’);
returns only one element, the first one
document.querySelectorAll(‘input’); // NodeList of elements, all findings, array-like object
a little less performant than getElementsBy… but not drastically enough to avoid
address text of the element
h1.innerText
change it in the console: h1.innerText = “Something new”
if you do ul.innerText (only has children, no direct text) - will give you the text of all children
document.body.innerText gives you all the text from the page
textContent is the same, but retrieves formatting - line breaks, inner texts of embedded elements (<b>)
faster than innerText. Vomits out everything coming between the tags, no matter what inside
</b>
innerHTML
gives the HTML representation of everything between the tags of the element, as string
innerText by comparison does not give you html tags
if you set/update content - with innterText you can only insert the text, the literal string
with innerHTML you can insert an HTML piece
get content of field
input.value
checkbox checked or not
input.checked
change placeholder
input.placeholder = “Please, enter password”
change link
a.href
image path
image.src
value of the attribute of the element
element.getAttribute(‘type’)
access parent
element.parentElement
supports chaining
element.parentElement.parentElement
access children
element.children[0]