Document Object Model (For JS) Flashcards

1
Q

Types of Javascript in Website

A

Inline - This works similar to inline CSS, but instead of ‘style’ property, we use [ onload = “ js-command( ‘ argument ‘ ) “ ]
Internal - When scripts are included in the HTML, right at the end - before the closing body tag - and b/w open/close ‘script’ tag.
External - When an external file is linked to HTML document, we call it external JS.

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

DOM

A

Helps in categorizing all the elements in an HTML so they can be selectively targeted by javascript in order to modify them.

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

Some frequent commands

A

some-dom-element-parent . firstElementChild = selects the first child of a parent DOM targeted
some-dom-element-parent . lastElementChild = selects the last child of a parent DOM targeted
some-dom-element . click ( ) = simulates a click on targeted element
some-dom-element . innerHTML = changes the HTML thats being targeted
some-dom-element . textContent = changes the content for the element targeted, but no change in HTML.

document . querySelector ( ‘HTMLtag / class / id’ ) = Targets the first occurence of element mentioned w/i bracket
document . querySelectorAll ( ‘HTMLtag / class / id’ ) = Targets all occurence of the mentioned element and returns an array as o/p

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

What should I always remember about querying for an DOM object?

A

The fact that when I query for an object - the o/p might be an array and to access the element, I will need to specify array index, without which - I will not get the expected result - and worse, will waste a bunch of time not knowing where I am doing wrong.

Its a good idea to open up the browser and use the console window to target and ‘get’ the properties of the particular element first before seeing how to change them.

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

Changing CSS from JS

A

document.querySelector( ‘tag’ ) . style . css-property-camelCased = “the value passed as string”

Example -

document.querySelector( ‘div’ ) . style . backgroundColor = “midnightblue”;

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

Query Class List and keeping stylesheeet separate from JS

A

Can be used to list all the classes attacked to an element.

document. querySelector(“HTMLtag/class/id”).classList = Lists the classes related to the target object
document. querySelector(“HTMLtag/class/id”).classList.add = self explanatory
document. querySelector(“HTMLtag/class/id”).classList.remove = self explanatory

These can be used to keep the CSS sheet separate from Javascript.

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

Getting Attributes and Setting Attributes

A

document. querySelector(‘HTMLtag/class/id’).getAttribute(‘the-property-to-get’);
document. querySelector(‘HTMLtag/class/id’).setAttribute(“the-property-to-set” , “what-to-change attribute-to”) ;

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

Add Event Listener

A

Dom-object-target.addEventListener (“type-of-event” , firstFunct );

function firstFunct ( arguments )
{
       code here;
}

OR

dom-object-target.addEventListener (“type-of-event” , function ( ) { code here; } );

The purpose of this METHOD is to listen to type of event specified as first parameter and then run the function specified as second parameter.

type-of-event —–> https://developer.mozilla.org/en-US/docs/Web/Events

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

Higher order function

A

when a function calls another function as an argument.

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

Play Audio

A
var sound = new Audio( 'location of track' );
sound.play( );

play( ) is method to play a sound.

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

Creating an ‘Object’

A

Object is a kind of data type, which helps us to store multiple other types of data namely - multiple string, integer and boolean.

Example

let myVar = {
property1: value1;
property2: value2;
property3: string1;
}

And to access one of these property, we pass the following command:

myVar . property1

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

Initialize an Object Constructor Function

A

let myVar = new MyVar ( “values-here-separated-by-comma”, “like here in the example” );

function MyVar ( "passed arguments from where this is called", "gets caught here")
{
               this.property1 = passed arguments from where this is called;
               this.property2 = gets caught here;
               this.method1 = function( ) {   //code here;   } 
}

So when I pass myVar.method1 —–> It will execute the function which is written in method1.

NOTICE - the function is not Camel-cased anymore, and first letter is capitalized. That is how we differentiate between object name and function name!

NOTICE2 - the constructor function can also have a function too that can be called to perform an operation.

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

Switch Case

A

When if-else become longer and very specific - use Switch case.

switch ( variable )
{
   case value-of variable for 1st case:
   {
        code here;
        .
        .
        break;      // break statement is very important here.
   }
   case value-of variable for second case:
   {
        code here;
        .
        .
        break;      // break statement is very important here.
   }
   default:              //when none of the case match
   {
        code here;
        .
        .
        break;      // break statement is very important here.
   }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Simple animation with JS and timeout

A

Simple animation with JS can be doneby adding/removing classes with different CSS, hence changing the look. Timing it with the setTimeout( function ( ) { } , delay ) method - we can do some simplistic animation.

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