JS removeChild, innerText and Events Flashcards

1
Q

How do we remove an element using getElementbyId?

A

remove();

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

Using removeChild() tell me the steps you need to take to use it in this code:

<body>
<h1>Hello!!!!</h1>
<div>
<h1>Laura</h1>
</div>
</body>

A

1) create a variable for parent and child element.
2) Target child element with querySelector not ‘document’ to target the specific child and put into variable.
3) finally use parent.removeChild(child);

VOILA

const name = document.querySelector(‘#name’);

const nameHeading = name.querySelector(‘h1’);

name.removeChild(nameHeading)

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

What’s the difference between innerHTML and textContent? In this code:

<body>
<h1>Hello!!!!</h1>
<div>
<h1>Laura</h1>
</div>
</body>

A

const name = document.querySelector(‘#name’);

console.log(name.innerHTML)

By using innerHTML, we can set the HTML content of the element. including elements not just text.

E.g name.innerHTML = <h2>Laura is ma <em>name</em></h2>;

with name.textContent = name.textContent = “Nikki”

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

Why does this not work?
const btn = document.getElementsByTagName(‘button’);

btn.addEventListener(‘click’, function() {
btn.textContent = “Click me again”;
});

A

TagName returns a collection of html elements - so you will need to specify index:

HERE:
const btn = document.getElementsByTagName(‘button’)[0];

btn.addEventListener(‘click’, function() {
btn.textContent = “Click me again”;
});

console.log(btn);

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

Tell me why for a button click event you need a ‘call-back function’? How is this different to other Javascript on the page?

A

JS usually synchronous. Callback functions are asynchronous and don’t happen sequentially. So you have to ‘wait’ until a button is clicked/event happens to run a function.

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

What are the ‘arguments’ for click events?

btn.addEventListener(‘’”, function() {

});

A

1) specify event e.g on hover/on click then the function to be executed.

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

How would you add a class to the heading:

<body>
<button>CLICK ME TO REVEAL MY NAME</button>
<h1></h1>
</body>

A

const btn = document.getElementsByTagName(‘button’)[0];

const heading = document.getElementsByTagName(‘h1’)[0];

ADD CLASS =
btn.addEventListener(‘click’, function() {
heading.textContent = “LAURAAAA”;
heading.classList.add(“green”);

});

CSS: .green {
color:green;
}

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

Why would this not be the correct way to run the callback function? And how can we check when it will run? Synchronous or asynchronous.

const btn = document.getElementsByTagName(‘button’)[0];

const heading = document.getElementsByTagName(‘h1’)[0];

function changeColoursRevealName(){
heading.textContent = “LAURAAAA”;
heading.classList.add(“blue”);
}

THIS PART:

btn.addEventListener(‘click’, changeColoursRevealName());

console.log(btn);

A

Because it would call it immediately with JS. It would not wait for button click.

For that do this:

btn.addEventListener(‘click’, changeColoursRevealName);

Without round brackets.

TO CHECK:

use a console.log inside the function.

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

<!DOCTYPE html>

<html>
<head>
<link></link>
<link></link>
</head>
<body>
<!-- Rest of your HTML content -->
</body>
</html>

Which style sheet will override which? When is it useful?

A

The one furthest down otherStyle.

When you have a ‘base’ style and then you want to add new styles above that.

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

Using this code, create a on click function which reveals the text:

<h1>Hello</h1>

<button>Show Text</button>

<p>This text will appear when the button is clicked.</p>

CSS:

.invisible {
display: none;
}

const btn = document.getElementById(“showButton”);

const text = document.querySelector(“.hiddenText”);

function showText(){
if(text.classList……..(“hiddenText”)){
text.classList……(“hiddenText”);
} else {
text.classList……(“hiddenText”);
}
}

btn.addEventListener(“click”, showText);

A

const btn = document.getElementById(“showButton”);

const text = document.querySelector(“.hiddenText”);

function showText(){
if(text.classList.contains(“hiddenText”)){
text.classList.remove(“hiddenText”);
} else {
text.classList.add(“hiddenText”);
}
}

btn.addEventListener(“click”, showText);

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

What is the difference assignment operator = and === equality operator?

A

One assigns a value, the other checks a boolean if it evaluates to true or not.

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

What is the difference between mousedown and moouseup and click?

A

It gives you more control to break the ‘click’ up into two parts. E.g controller in a game.

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

What is the syntax for eventListener with mousedown and up?

A

element.addEventListener(“mousedown”, speedUp);

function speedUp(){
console.log(“You have sped up”)
}

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

What’s the difference adding an event which is mouse enter and mouse leave compared to mouse down mouse up?

A

mouse enter and leave is just ‘hover’;

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

what does addEventListener(“keypress”, showNumber); do?

A

in an input field e.g ( what’s your name?) Every key you press in this case with a letter, the function runs.

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

How can I check if a function is running?

A

Use a console log

17
Q

Why might a function not run?

A

The default has stopped it from running, or you have already called the function () instead of just a reference to it in the event listener e.g

addEventListener(“keypress”, showNumber()); - runs synchronously e.g immediately.

addEventListener(“keypress”, showNumber); - RUNS AFTER EVENT OCCURS.

18
Q

What is the difference between keypress, keydown and keyup?

A

key pressed - whenever pressed, only when held down, only when released.

19
Q

What is btn.addEventListener(function(event) {
console.log(event.currentTarget)
}

What does event.type do?

A

<button>Show Text</button>

shows type - e.g click or submit

20
Q

What is the default behaviour of internal link if it’s just href=”#”?

In what case is it good to prevent it? in HTML

A

Take you back to top of page.

Forms - can lose data/ take back to top of screen/no error/ success message/ page refresh.

21
Q

event.target or currentEvent?

This property refers to the actual element that triggered the event. It represents the specific element on which the event occurred.

how are they different?

A

event.target. - happens on whatever triggered the event, could be emphasised text/ image within a button.

event.currentEvents is what the event listener is added to. E.g button.

22
Q

What is event propagation?

A

It is an event being detected, triggered and propagated throughout elements. Usually nested. E.g grandparents, parents, children

23
Q

What is event bubbling?

A

It is when the event is triggered in multiple layers, with the inner most first e.g you have a button inside a div - both have a click event. When the button is clicked, the event is handled there, NEXT the event is handled on the DIV its parents. This is because it is default and nested inside.

24
Q

What is event capturing?

A

Opposite of bubbling. When I click on the child, the parent event is handled first, then goes down to child. Useful for form validation. Validates parent element first, then goes down.

25
Q

Which is the default - capturing or bubbling? And how do I ‘prevent’ default?

A

Bubbling, preventDefault().

26
Q

How can you ‘add’ event capture?

A

by adding a 3rd argument to true.

27
Q

Can an event listener be added to an element which was added dynamically?

A

NO

28
Q

If event listener cannot be added to an element added dynamically what can you do instead?

A

Target it’s ‘static’ parent element, and use does its class list contain “header” or “child element” if yes do this.

29
Q

on a form what action are we doing?

A

submit

30
Q

What is the default action when you click form submit?

A

Send info to backend server and clear and refresh page.

31
Q

What would I add to this eventListener to prevent default?

form.addEventListener(“submit”, function (e) {
console.log(“form submitttttteeeed”);
});

A

form.addEventListener(“submit”, function (e) {
e.preventDefault();
console.log(“form submitttttteeeed”);
});

32
Q

How would I access and console log the values of name input and password input?

<form>
<input></input>
<input></input>
<input></input>
<input></input>
</form>

A

form.addEventListener(“submit”, function (e) {
e.preventDefault();
console.log(“form submitttttteeeed”);
console.log(name.value);
console.log(password.value);
});

33
Q

Why does a warning come up saying ‘name is deprecated’ here and what does it mean? What should you do instead?

form.addEventListener(“submit”, function (e) {
e.preventDefault();
console.log(“form submitttttteeeed”);
console.log(name.value);
console.log(password.value);
});

A

Because Javascript Window object has that reserved, so it may conflict.

Deprecated means it is no longer recommended for use and may be deleted down the line.

Should change the ‘name’ to ‘inputName’ for e.g.