DOM Flashcards

1
Q

in css how to target input field but only of type password

A

input[type=”password”]{
height: 50px;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

address element by id

A

document.getElementById(‘id’);

null if not found

can only be called on document

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

find all fields on the page

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

find elements by class

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

find by css identifiers

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

address text of the element

A

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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

innerHTML

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

get content of field

A

input.value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

checkbox checked or not

A

input.checked

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

change placeholder

A

input.placeholder = “Please, enter password”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

change link

A

a.href

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

image path

A

image.src

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

value of the attribute of the element

A

element.getAttribute(‘type’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

access parent

A

element.parentElement

supports chaining
element.parentElement.parentElement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

access children

A

element.children[0]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

access siblings

A

element. nextElementSibling
element. previousElementSibling

supports chaining

17
Q

change style

A

element.style works only for inline styles, not for css files
but you can set it h1.style.color=”red”

IMPORTANT! JS does not allow dashes in var names, but CSS does
so for ex., background-color is a CSS property, but we can not ask JS to give us p.style.background-color

JS needs camel case instead: p.style.backgroundColor

18
Q

two ways NOT to add event listeners

A
  1. in HTML directly

Click me

  1. in JS
const btn = document.querySelector('#clicker');
btn.onclick = function(){
  alert("Clicked");
}

onclick is a property of the element. If we use it second time, the second will overwrite first, so we can bind only one function!!

if you do this:
btn.onclick = alert(“Clicked”); —-> the alert popup will come up without clicking, because you are calling the function

19
Q

the correct way to add events

A

const btn = document.querySelector(‘#clicker’);

btn.addEventListener(‘click’, function(){
console.log(‘SECOND’);
})

btn.addEventListener(‘click’, function(){
alert(‘SECOND’);
})

the method exists for all elements and opposed to previous methods (btn.onclick) - we can specify multiple functions on the same object for the same (or different) event separately and all of them will fire

20
Q

scroll on entire window

A

window.addEventListener(‘scroll’, function(){})

window is available as object

we can also scroll on other objects

21
Q

addEventListener and this

A

in the callback that we pass to addEventListener as second argument we have access to this. In this context, this is the object that we added event listener to (button)

const printColor = function(){
  console.log(this.style.backgroundColor);
}
const box = document.querySelector('#boxId');
box.addEventListener('click', printColor); // when we click and printColor is invoked, in its context this equals to the box element
22
Q

event object

A

in the callback (second argument of addEventListener) we can pass as argument the event object and use it in the body

const printColor = function(event){
console.log(this.style.backgroundColor);
console.log(event);
}

const box = document.querySelector('#boxId');
box.addEventListener('click', printColor);  

or like this

box.addEventListener(‘click’, function(event){
console.log(event);
});

23
Q

if we need to do something on user typing in something into a field

A

keypress, keyup, keydown

keyup, keydown work on every press of any button

keypress works on keys which when pressed show some content (for ex., shift does not work, but it’s better to check docs for diff browsers, because this rule does not always apply)

through event we can figure out which button was clicked and define actions on conditions

field.addEventListener('keypress', function(event){
  if(event.key === 'Enter'){
    //do somehting
  }
})

======

we can also observe input events with event input. Here we are updating form data with input text

field.addEventListener(‘input’, function(event){
formdata[cardNumer] = event.target.value;
})

for text fields and dropdowns we use event.target.value
for checkboxes event.target.checked

24
Q

submitting form

A

when you click the submit button, (by default) the page refreshes. This can be prevented.

const form = document.querySelector('#someform');
form.addEventListener('submit', function(e){
  e.preventDefault();
})
25
Q

elegant way to store form data when user inputs something in any input field, checkbox or dropdown

A
  1. on each html element that we are interested in we define name property with the value that is the key in our formData object
2. identify input fields
const textField = document.querySelector('#fid');
const checkbox = document.querySelector('#cid');
const dropdown= document.querySelector('#did');
  1. loop over these fields and add event listener to each of them. It should put the current value from the field into form data under the key of the element’s name.
    (Achtung: destructuring is used twice)

for(let input of [textField, checkbox, dropdown]) {
input.addEventListener(‘input’, ({target}){
const {name, type, value, checked} = target;
formData[name] = type === ‘checkbox’ ? checked : value;

})
}

target is event.target

if we use event change instead of event input:
for dropdown and checkbox it’s the same
but for textfield similar: it will update the form not on every key press, but on every focusing and losing focus.
So for ex., after I entered smth and then pressed Enter or just switched focus.
event input updates on every key press