Altera ep4ce6e22c8

We often get asked how much an Altera ep4ce6e22c8 device costs. The answer, unfortunately, is not as simple as a set price. There are a few different factors that come into play when determining the…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Using Javascript to manipulate HTML elements via the DOM

As you might be familiar, HTML and CSS are not ‘programming’ languages in the true sense. HTML is a ‘markup’ language that is used to create and display the elements of a webpage. CSS is a ‘stylesheet’ language that is used to write rules that will dictate the style and appearance of said elements. Although CSS does allow for some pseudo-programming with the help of pseudo-classes, it still isn’t considered as a programming language. Moreover, the functionality of CSS to manipulate elements during runtime is limited.

This is where Javascript comes in. It was initially created to be a programming language for the web browser, allowing us to be able to observe and manipulate the HTML and CSS code at run-time. The web browser interacts with Javascript by providing it a ‘host environment’ to run its commands on. This host environment provides certain objects and additional functions, which allow us to access additional features of the browser and elements of the HTML page at run-time.

At the root of the browser, we have the window object. The window object is a global object in Javascript and provides methods to control the browser window through it. Here are some examples to make you understand better

The window object is further divided into three components, namely the DOM, the BOM, and Javascript objects. We shall be further looking at how DOM works in this article.

The DOM is basically the container for all the objects that can be accessed and manipulated in a web page. Think of all the HTML elements in a web page as a collection of nested objects, for whom the root object is the DOM. The DOM is accessed in code by using thedocument keyword. So, we can access the <body> tag of the HTML page by calling document.body.

Now say that you wish to access some other element of the page, like say a <p> element. Before we get to that, we need to first understand how elements in a web page are mapped to their corresponding objects in the DOM. Let’s take the example of a simple page

The DOM treats every HTML element in the above page as a node. There are 12 different types of nodes. However, we shall be looking only at 4 of them for now.

In the above code, the <html> tag is an element node. DOM follows a tree structure while organizing nodes. Hence, the <head> and <body> tags are element nodes that are considered as the child nodes of the <html> tag.

Selecting the nodes of these tags in Javascript is pretty simple as there as built-in functions for the same

If we wished to select any other node, like say a <p> tag, DOM provides us with additional searching methods

This method allows us to select an element in the web page that contains a particular id attribute.

This method allows us to select the first element that matches the given CSS selector

This method allows us to select all elements that match the given CSS selector

This method allows us to select all elements of a particular tag

This method allows us to select all elements with the given class

Now that we have seen some basic methods to select elements in DOM, let us also see a few examples of how we can modify those elements too. Let us say we have a page like this

Let us say we wish to change the text of the first paragraph. We can do so by using the innerHTML property as follows

You can also add other tags within the value of innerHTML and the browser will render them as HTML text. For example, if you wished to change the text of the paragraph as well as make it bold, we can do something like this

In order to change the style of an element, we use the style property. For example, in order to set a simple border to the paragraph, we write

In this article, we have attained a basic understanding of how DOM works and how it can be used to manipulate HTML elements. In the coming weeks, I shall be adding more articles into the deeper workings of Javascript. Till then, happy coding!

Add a comment

Related posts:

The Path in Destiny Through Reality or Imaginary World

We are acting in a movie in which its screenplay was written by us long before, that is so-called destiny. But if we don’t remember the scenario, then bad characters emerge and force us getting worse…

Freelancer jobs online and 5 ways blockchain will integrate with freelancer marketplaces.

Working freelancer jobs online is one of the fastest growing industry of our time. Welcome to a journey I have taken down into the most revolutionary industries on the planet. We have first of all…

Un anel de prata

En 2020 trasladeime á trixésima casa da miña vida o mesmo día que celebrei o meu trixésimo aniversario. Este é o único dato que me ven á cabeza cando me preguntan polo meu 2020 e nin sequera é certo…