Javascript 2 Flashcards

(13 cards)

1
Q

As an HTML attribute

A

<body>
</body>

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

As a method

A

window.onclick = myFunction();

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

major drawback of using methods:

A
  • we can bind only one event at a time with this method.
  • The second binding overwrites the first, so when the
    user clicks inside the browser window, only
    myOtherFunction() will run.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

addEventListener

A
  • This approach allows us to keep our logic
    within our scripts and allows us to perform
    multiple bindings on a single object.
  • We start by calling the addEventListener
    method of the target object, and then
    specify the event in question and the
    function to be executed as two arguments.
    – window.addEventListener(“click”, myFunction);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Document Object Model (DOM)

A
  • The DOM gives us a way to access and
    manipulate the contents of a document (i.e.,
    html document).
  • It provides a structured map of the
    document, as well as a set of methods to
    interface with the elements contained
    therein.
    – We can use it to find elements by their names or
    attributes, then add, modify, or delete elements
    and their content
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

DOM as document tree

A
  • Each element within the page is referred to as a node.
  • Each node can contain further branches.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Accessing DOM nodes

A

– The document object in the DOM identifies the
page itself.
– The document object comes with a number of
standard properties and methods for accessing
collections of elements.
– The document object comes with a number of
built-in properties containing information about
the document.

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

getElementById()

A

var foo = document.getElementById( “beginner” ).innerHTML;
* the statement says to look on the page (document), find the
element that has the id value “beginner”, find the HTML content
within that element (innerHTML), and save those contents to a
variable (foo).

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

getElementsByTagName()

A

– document.getElementsByTagName(“p”) returns every
paragraph on the page, wrapped in something called a
collection or nodeList, in the order they appear in the
document from top to bottom.
– nodeLists behave much like arrays.
- To access specific paragraphs in the nodeList, we reference
them by their index, just like an array.

  • var paragraphs = document.getElementsByTagName(“p”);
  • paragraphs[0] is a reference to the first paragraph in the document
  • paragraphs[1] refers to the second
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

getElementById()

A

– <imgsrc=”photo.jpg” alt=”” id=”lead-photo”>’
– var photo = document.getElementById(“lead-photo”);

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

getAttribute()

A

– <imgsrc=”stratocaster.jpg” alt=”electric guitar” id=”lead
image”>
– var bigImage = document.getElementById(“lead-image”);
– alert( bigImage.getAttribute(“src”) ); // Alerts
“stratocaster.jpg”.

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

getElementsByClassName()

A

– this allows you to access nodes in the document based
on the value of a class attribute.
* <div class="example">Element1</div>

<div>Element2</div>

<script>
const collection = document.getElementsByClassName("example");
 collection[0].innerHTML = "Hello World!";
</script>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly