FSD Notes Module-2
FSD Notes Module-2
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.
Accessing and updating the DOM tree involves two steps:
1: Locate the node that represents the element you want to work with.
2: Use its text content, child elements, and attributes.
Step-1: Access the elements
Several methods let you create new nodes, add nodes to a tree, and remove nodes
from a tree: create Element(), createTextNode() ,appendChild() / removeChild()
This is called DOM manipulation.
c. Access or update attribute values: Here are some of the properties and methods
you can use to work with attributes: className /id.
get or update the value of the cl ass and id attributes. hasAttribute()
getAttribute() setAttribute() removeAttribute() The first checks if an attribute
exists. The second gets its value. The third updates the value. The fourth removes
an attribute.
DOM Manipulation: DOM manipulation refers to a set of DOM methods that allows you to
create element and text nodes, and then attach them to the DOM tree or remove from the DOM
tree.
Or
DOM manipulation refers to using a set of methods and properties to access, create, and update
elements and text nodes.
DOM manipulation offers another technique to add new content to a page (rather than
innerHTML).
ADDING ELEMENTS:
It involves three steps:
1. Create the element: creating a new element node using the
createElement() method. This element node is stored in a variable. When
the element node is created, it is not yet part of the DOM tree. It is not
added to the DOM tree until step 3.
2. Give it content: createTextNode() creates a new text node. Again, the
node is stored in a variable. It can be added to the element node using the
appendChild() method. This provides the content for the element,
although you can skip this step if you want to attach an empty element to
the DOM tree.
3. Add it to the DOM: you can add it to the DOM tree using the appendChi1d ()
method. The appendChi1d() method allows you to specify which element you
want this node added to, as a child of it.
Example:
REMOVING ELEMENTS: DOM manipulation can be used to remove elements from the DOM tree.
• Store the element to be removed in a variable: selecting the element that is going to
be removed and store that element node in a variable.
• Store the parent of that element in a variable: Next, you find the parent element that
contains the element you want to remove and store that element node in a variable. The
simplest way to get this element is to use the parentNode property of this element.
• Remove the element from its containing element: The removeChild() method is used
on the containing element that you selected in step 2. The removeChild() method takes
one parameter: the reference to the element that you no longer want. Removing elements
from the DOM will affect the index number of siblings in a Nodelist
Example:
ADVANTAGES
• It is suited to changing one element from a DOM fragment where there are many siblings.
• It does not affect event handlers.
• It easily allows a script to add elements incrementally (when you do not want to alter a lot of
code at once).
DISADVANTAGES
• If you have to make a lot of changes to the content of a page, it is slower than innerHTML.
• You need to write more code to achieve the same thing compared with innerHTML.
Selecting an element from a nodelist:
• There are two ways to select an element from a Nodelist:
• The item() method and array syntax.
• Both require the index number of the element you want.
1.THE item() METHOD:
• Nodelists have a method called item() which will return an individual node from the Node list.
• Select elements that have a cl ass attribute whose value is hot and store the Nodelist in variable
called elements.
• Use the 1ength property to check how many elements were found. If 1 or more are found, run
the code in the if statement.
• Store the first element from the Node List in a variable called first item. (It says 0 because
index numbers start at zero.)
2. Array Syntax:Array syntax is preferred over the item() method because it is faster.
• Access individual nodes using a square bracket syntax similar to that used to access
individual items from an array.
• Specify the index number of the element you want inside the square brackets that follow
the NodeList.
• If you create a variable to hold a NodeList but there are no matching elements,the
variable will be an empty NodeList.
• When you check the length property of the variable,it will return the number 0 because it
does not contain any elements.
• The getElementsByTagName () method allows you to select elements using their tag name.
• The element name is specified as a parameter, so it is placed inside the parentheses and is
contained by quote marks.
• Note that you do not include the angled brackets that surround the tag name in the HTML (just
the letters inside the brackets).
Example:
Removing attributes:
• To remove an attribute from an element, first select the element, then call removeAttr ibute (). It
has one parameter: the name of the attribute to remove.
Example:
EVENT LISTENERS:
• Event listeners are a more recent approach to handling events. They can deal with more than one
function at a time but they are not supported in older browsers.
• Here is the syntax to bind an event to an element using an event listener, and to indicate which
function should execute when that event fires:
EVENT DELEGATION:
•Creating event listeners for a lot of elements can slow down a page, but event flow allows you to
listen for an event on a parent element.
• If users can interact with a lot of elements on the page, such as:
• A lot of buttons in the UI
• A long list
• Every cell of a table adding event listeners to each element can use a lot of memory and slow
down performance.
• Because events affect containing (or ancestor) elements), you can place event handlers on a
containing element and use the event object's target property to find which of its children the
event happened on.
• By attaching an event listener to a containing element, you are only responding to one element.
ADDITIONAL BENEFITS OF EVENT DELEGATION
• Works with new elements:
If you add new elements to the DOM tree, you do not have to add event handlers to the new
elements because the job has been delegated to an ancestor.
• Solves limitations with this keyword:
Earlier in the chapter, the this keyword was used to identify an event's target, but that technique
did not work in IE8, or when a function needed parameters.
• Simplifies your code:
It requires fewer functions to be written, and there are fewer ties between the DOM and your
code, which helps maintainability.
ii)Load Event: The load event is commonly used to trigger scripts that access the contents of the page.
In this example, a function called setup() gives focus to the text input when the page has loaded.
MANJUNATH J P,ASST.PRO DEPT.ISE,VEMANA IT Page 10
FULLSTACK DEVLOPMENT
iii)Focus & Blur Events: The HTML elements you can interact with, such as links and form elements,
can gain focus. These events fire when they gain or lose focus.
Example:
Mouse events:
• The mouse events are fired when the mouse is moved and also when its buttons are clicked.
• All of the elements on a page support the mouse events, and all of these bubble. Note that actions
are different on touchscreen devices.
Click event:
Keyboard events: The keyboard events are fired when a user interacts with the keyboard.
Example:
Form Events: There are two events that are commonly used with forms. In particular you are likely to
see submit used in form validation and focus and blur .
HTML5 events: Here are three page-level events that have been included in versions of the HTML5
Example :