Learn DOM Nodes and Properties in JavaScript

0
2634
Learn DOM Nodes and Properties in JavaScript

Learn DOM Nodes and Properties in JavaScript

DOM stands for Document Object Model. When an HTML file is loaded into browser it become a webpage and then browser create DOM of that webpage. By using DOM, JavaScript can do anything with webpage and converts the webpage into dynamic HTML. In JavaScript, The Document Object Model looks like a tree of objects shown below.

1
In this tree, the highest tag called the top node.

What JavaScript can do with DOM in our HTML document?

  • JavaScript can change any text from any HTML tag.
  • JavaScript can change HTML elements and its attributes as well.
  • JavaScript can remove any HTML element and can add new HTML elements as well.
  • JavaScript can be effective for HTML events.
  • JavaScript has the full authority to change, remove and add new CSS styles.
  • It actually implements new styles from itself.
  • JavaScript considered every HTML element as an object. So it works with HTML in the way of objects.
  • All of the DOM consist of Element as an object and its nodes.
  • Simply JavaScript interaction with HTML tags, attributes and CSS styles is called DOM.

Note: DOM is case sensitive! So be careful! Place small letter where you find some small letters, and place capital letter where you find capital letters in the examples below.

Why

Sometimes we need to change things in our website according to the users need. We also want to print or get some JavaScript results to an HTML element.
For example, we made a function which can add 2 values and to get the result in a stylish way. So we can directly print our results to an HTML element by using the DOM. Here is a working example:

In the above example, I’ve made 2 variable with some values. In the next variable, I’m getting total sum of that 2 variables. And in the last line of code, I’m accessing to an HTML element by using DOM and applying a method .innerHTML to that HTML element which is show below.

Every JavaScript programmer knows about the writing method in JavaScript because it’s a common thing to write anything on the page using JavaScript.

Above method is also a part of DOM. Document is the object which is considered writing to the whole web page and write(); is a method to write on the document.

Above method is a another method in JavaScript to create an alert box to the browser window. This method uses window object and effects on the whole window not just the document. Mean it can hang complete browser window. Maybe you notice that you even can’t refresh your page if an alert has got active.

Changing text

For changing simple text from HTML through JavaScript, we have to use innerHTML method as we discussed before. But we can use it in different ways. We can select the HTML element by using its tag name, It ID or with its class name. Let me explain how!

First, we just made three HTML elements, the first element has an ID, and the second element has a class and the third element has just its tag name and nothing.

To select an HTML element by using its id:

To select an HTML element by using its class name:

To select an HTML element by directly using its tag name:

We can also store these informations in JavaScript variables:

These variables are completely usable.

We’ve done the same thing here in the above example as we did in some previous examples. But what is new is just we actually declared a variable and assigned the HTML object with that variable and then perform actions by using innerHTML method.
Instead of updating, we can remove text from any tag as well. Just leave the innerHTML’s property empty:

Changing attribute value:
To change the attribute value of any HTML element without variable:

I used an image to show how to change an attribute’s value by using DOM method but we can change any attribute at all as per the requirement:

In the above example I changed the border attribute from the table tab which has table1 id.

Changing CSS value:
You have to do a plenty of things if you want to enjoy CSS with DOM.
The property for CSS is called .style.property = new style in DOM.

Note: CSS properties which has two-words like background-color, margin-top, box-shadow must be written in camel style without hyphen like backgroundColor, marginTop, boxShadow etc…

Camel style writing is a special style to write and code something in any language. Camel style text must consist of two or more words. It can be used when we are declaring a variable, array, function or something like this. But it’s necessary to use camel style here in Dom with style property. Because this is the only way declared in JavaScript.

Thus we have finished learning on DOM Nodes and Properties in JavaScript, You can post your queries regarding this topics in comments sections below.

LEAVE A REPLY

Please enter your comment!
Please enter your name here