How to remove a HTML data attribute in JavaScript
In this post we will give you information about How to remove a HTML data attribute in JavaScript. Hear we will give you detail about How to remove a HTML data attribute in JavaScriptAnd how to use it also give you demo for it if it is necessary.
we are going to learn how to remove the HTML data attributes using JavaScript.
Consider, we have the following html element with the data attribute data-color=”green”:
<div data-color="green" >First div</div>
Now, we need to remove the above element data attribute in JavaScript.
Removing the data attribute
To remove the data attribute from an html element, first we need to access it inside the JavaScript using the document.querySelector() method.
const el = document.querySelector(".row");
Now, it has a removeAttribute() property which is used to remove the specified attribute from an element.
el.removeAttribute("data-color");
We can also remove the multiple data attributes from a html element like this:
const el = document.querySelector(".row");Object.keys(el.dataset).forEach(key=>{ delete el.dataset[key];})
Hope this code and post will helped you for implement How to remove a HTML data attribute in JavaScript. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs