DEV Community

Hadil Hijazi
Hadil Hijazi

Posted on

innerHTML property in JavaScript

InnerHTML property in Javascript

For the past few weeks, I have been learning about JavaScript in the context of Web Development. JavaScript is a very important programming language for the Web. HTML and CSS can be updated via JavaScript. It is also the most commonly used programming language world wide.

InnerHTML property is a method in JavaScript that users use to clear the contents div in HTML. This method is useful in Web development because it solves a minor issue. For example, I was working on an assignment, where I encountered an error after writing my code. The assignment I was working on required me to display details of a certain item when it gets clicked on. Each time I clicked on a specific item, the page would refresh and display all the details of every item in the list.

I knew that I was missing something and here is where I figured out clearing the content of div in HTML. I then figured out that I needed to add .innerHTML = "". in my function. This was a great lesson I learned about clearing the contents of a specific div element.

If for example we are talking about a Robots web page. And we have a list of Robots, within each Robot name we have a picture of and some details about it.

This is an example of the index.html file

Image description

We can insert a . innerHTML = "" in the displayRobotDetails function.
The function would look something like this :

Image description

In this scenario, we are using the .innerHTML = "" in order to clear the content dynamics. We have to do this so that we get the details/images or other data we want separately. In this case, when we click on a robot name from the web page, we would only be able to view the details of that specific robot. This method is one of two methods available to clear the contents of the div.

Top comments (0)