reading-notes


Project maintained by Razan-am Hosted on GitHub Pages — Theme by mattgraham

JAVASCRIPT

Chapter 3: “Object Literals”

Example:

  • This object represents a hotel. It has five properties and one method.The object is in curly braces. It is stored in a variable called hotel .

var hotel = {

//PROPERTIES

These are variables

name : 'Quay';

room : 40;

booked : 25;

gym : true;

roomTypes : ['twin' , 'double' , 'suite'];

//METHOD

This is a function

checkAvailability : function (){

` return this.room - this.booked ; }`

};

The example above shows how to creat an object by letiral notation (There is a several ways to creat an object)

To access the property from the example above:

var hotelName = hotel.name;

//or

var hotelName = hotel ['name'] ;

//hotel is the object

// name is the property

To access the method from the example above:

var roomsFree = hotel.checkAvailability ();

Example:

var hotel = {

name: 'Park',

rooms: 120,

booked : 77,

checkAvailabi lity: function() {

return this.rooms - this.booked; }

} ;

var elName = hotel .name ;

var el Rooms = hote 1 . checkAvai l abi lity();


Chapter 5: “Document Object Model”

The DOM is a separate set of rules, so it is not only part in html or only in javascript.

1. Making a model for an html page

2. Accessing and changing the HTML page


THE DOM TREE IS A MODEL OF A WEB PAGE

  1. THE DOCUMENT NODE

    Every element, attribute, and piece of text in the HTML is represented by its own DOM node.At the top of the tree a document node is added; it represents the entire page.

  2. ELEMENT NODES

    The HTML elements that describe the structure of an HTML page ,like the <hl> lements describe what parts are headings; the <p> tags indicate where paragraphs of text start and finish; and so on.

To access the DOM tree, you start by looking for elements. Once you find the element you want, then you can access its text and attribute nodes if you want to. This is why you start by learning methods that allow you to access element nodes, before learning to access and alter text or attributes.

Each node is an object with methods and properties. Scripts access and update this DOM tree (not the source HTML file).Any changes made to the DOM tree are reflected in the browser.

  1. ATTRIBUTE NODES

    The opening tags of HTML elements can carry attributes and these are represented by attribute nodes in the DOM tree.

The attribut is not the children of the element it is part of the element.

  1. TEXT NODES

    Once you have accessed an element node, you can then reach the text within that element. This is stored in its own text node.


WORKING WITH THE DOM TREE

Finding the elements

1. ACCESS THE ELEMENTS

2. WORK WITH THOSE ELEMENTS

  1. Access/update text nodes:The text inside any element is stored inside a text node, and to access it :
    1. Select the parent element
    2. Use the firstChild property to get the text node
    3. Use the text node’s only property (nodeValue) to get the text from the element.

nodeValue:this property lets you access or update contents of a text node.

  1. Work with HTMl content :
    1. innerHTML : allows access to child elements and text content
    2. textContent :Allows access just to the text content
    3. create Element()/createTextNode()/ppendChild () / removeChild () :these are a several methods let you create new nodes, add nodes to a tree, and remove nodes from a tree, it is called DOM manipulation.
  2. Access or update attribute values :
    1. className /id :Lets you get or update the value of the cl ass and id attributes.
    2. hasAttribute() : checks if an attribute is exists
    3. getAttribute() : to gets the attribute value.
    4. setAttri bute() : it is to updates the attribue value.
    5. removeAttribute() : it is to removes an attribute.

ACCESSING ELEMENTS

Nodelist: several elements can have the same tag name, so getElementsByTagName () will alwaysreturn a Nodelist.And if you want to select the element from this list using an index number (which means the numbering starts at 0 like the items in an array).

Fastest route:to access an element within your web page quickly will make the page seem faster and/or more responsive.For example, getEl ementByld () will quickly return one element .

- Methods that return a single element node:

Example:

//By id attribute:

// Select the element and store it in a variable.

var el = document.getElementByid('one');

// Change the value of the class attribute.

el.className ='cool ' ;


- Methods that return one or more elements (as a Nodlist):

More about the Nodlist

Example:

var elements = document.getElementsByClassName('hot')

if (elements.length>= 1) {

var firstltem = elements.item(0); }

You can also access it also by using the [ ] seqaure barckets.

Example:

var elements = document.getElementsByClassName('hot')

if (elements.length>= 1) {

var firstltem = elements[0]; }


SELECTING ELEMENTS USING CLASS ATTRIBUTES

// Find hot items

var elements = document .getEl ementsByClassName('hot');

// If 3 or more are found

if (elements.l ength> 2) {

// Select the third one from the Nodelist

var el = elements[2];

// Change the value of its class attribute

el.className = 'cool';}

SELECTING ELEMENTS BY TAG NAME

//Find <li> elements

var elements = document.getElementsByTagName('li ');

// If 1 or more are found

if (elements.length> 0) {

//Select the first one using array syntax

var el = elements[0];

//Change the value of the class attribute

el.className = 'cool'; }

SELECTING ELEMENTS USING CSS SELECTORS

// querySelector() only returns the first match

var el = document .querySel ector('li .hot ' };

el.className = ' cool' ;

//querySelectorAll returns a Nodelist

//The second matching element (the third list item) is selected and changed

var els = document .querySelectorAll('li .hot') ;

els[1] .className = ' cool' ;

LOOPING THROUGH A NODELIST

// Store Nodel ist i n array

var hotlt ems = document .querySelectorAl l ('li.hot');

//If it contains item

if (hot ltems.length > 0) {

//Loop through each item

for (var i=0; i<hotItems.length; i++) {

//Change value of class attribute.

hotltems[i] .className = 'cool'; }

}

TRAVERSING THE DOM

<Traversing the DOM can be difficult because some browsers add a text node whenever they come across whitespace between elements.>

//Select the starting point and find its siblings

var startltem = document.getElementByid('two');

var prevltem startltem.previousSibling;

var nextltem = startitem.nextSibling;

//Change the values of the siblings' class attributes

prevltem.className 'complete ' ;

nextltem.className 'cool';


-To solve this problem for the firstChild/lastChild we use the getElementsByTagName().

Example:

//Select the starting point and find its children

var startltem = document.getElementsByTagName('ul')[0];

var firstltem = startltem. firstChild;

var lastltem = startitem.lastChild;

//Change the values of the children's class attributes

firstltem.setAttribute('class ' , 'complete');

lastitem.setAttribute('class', ' cool');


Access/update element content

- ACCESS and UPDATE A TEXT NODE WITH NODEVALUE

document.getElementByid( 'one' ).firstChild.nextSibling. nodeValue;

  1. The <li>element node is selected using the getElementByid () method.
  2. The first child of <li > is the <em> element.
  3. The text node is the next sibling of that <em> element.
  4. You have the text node and can access its contents using node Value.

- ACCESSING & CHANGING A TEXT NODE

//Get second list item

var itemTwo document.getElementByld('two');

//Get its text content

var elText itemTwo.firstChild .nodeValue;

//Change pine nuts to kale

elText = elText.replace( ' pine nuts', ' kale ');

//Update the list item

itemTwo .firstChi ld.nodeValue = elText;


- ACCESS & UPDATE TEXT ONLY WITH TEXTCONTENT

//Find first list item

var firstltem = document.getElementByld('one');

//Get value of textContent

var showTextContent = firstitem.textContent;

//Get value of innerText

var showinnerText = firstitem.innerText;

<An overall about the DOM>

@Jon Duckett/JAVASCRIPT