Manipulation of HTML elements with JavaScript is one of the most important skills for a web developer. This is because JavaScript is a programming language that allows the creation of interactivity and dynamism in a web page, and the manipulation of HTML elements is one of the main ways to achieve this.

To manipulate HTML elements with JavaScript, it is necessary to have a basic knowledge of HTML structure. Each HTML element is represented by a tag, which is a code between angle brackets (< and >). For example, the <p> represents a paragraph, while the <img> represents an image.

To manipulate an HTML element with JavaScript, you must first identify it. This can be done in several ways, but the most common is using the getElementById() method. This method receives as a parameter the ID of the element to be manipulated and returns a reference to that element. For example:


<div id="myDiv"></div>
<script>
var myDiv = document.getElementById("myDiv");
myDiv.innerHTML = "Hello world!";
</script>

In this example, the getElementById() method is used to get a reference to the <div> with id "myDiv". Then the innerHTML property of that element is changed to "Hello world!" which makes the text "Hello world!" be displayed inside the div.

In addition to the getElementById() method, there are other methods that can be used to identify HTML elements, such as getElementsByTagName() and getElementsByClassName(). The first returns a list of elements that have a given tag, while the second returns a list of elements that have a given class.

Once you identify an HTML element, you can manipulate it in a number of ways using JavaScript. Some of the most common properties that can be changed are:

  • innerHTML: defines or returns the HTML content of an element
  • value: sets or returns the value of a form element
  • src: sets or returns the URL of an image
  • href: sets or returns the URL of a link
  • style: sets or returns the style properties of an element

For example, to change the value of a form field, just use the following code:


<input type="text" id="myField">
<script>
var myField = document.getElementById("myField");
meuField.value = "New value";
</script>

In this example, the getElementById() method is used to get a reference to the form element with ID "myField". Then the value property of that element is changed to "New Value", which causes the value of the form field to change.

In addition to changing the properties of an HTML element, it is also possible to add or remove elements from the page using JavaScript. To add a new HTML element, simply create a new element using the createElement() method and add it to the page using the appendChild() method. For example:


<div id="myDiv"></div>
<script>
var myDiv = document.getElementById("myDiv");
var newParagraph = document.createElement("p");
newParagraph.innerHTML = "New paragraph";
myDiv.appendChild(newParagraph);
</script>

In this example, the createElement() method is used to create a new element <p> with the content "New paragraph". Then the appendChild() method is used to add this new element to the <div> with id "myDiv".

Finally, to remove an HTML element from the page, just use the removeChild() method. This method receives as a parameter the element to be removed and removes it from the page. For example:


<div id="myDiv">
  <p>Paragraph 1</p>
  <p>Paragraph 2</p>
</div>
<script>
var myDiv = document.getElementById("myDiv");
var paragraph2 = myDiv.getElementsByTagName("p")[1];
myDiv.removeChild(paragraph2);
</script>

In this example, the getElementsByTagName() method is used to get a list of all <p> inside the <div> with id "myDiv". Then the second element of that list (which corresponds to the paragraph with the text "Paragraph 2") is removed using the removeChild() method.

In short, manipulating HTML elements with JavaScript is an essential skill for any web developer. With it, you can create dynamic and interactive pages that offer a richer user experience.

Now answer the exercise about the content:

_What is the most common method for identifying HTML elements with JavaScript?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Events in HTML 14

Next page of the Free Ebook:

Events in HTML

Estimated reading time: 3 minutes

Download the app to earn free Certification and listen to the courses in the background, even with the screen off.

+ 9 million
students

Free and Valid
Certificate

60 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video and ebooks