by @kodeazy

why removeAttribute function in javascript is not working?

Home » javascript » why removeAttribute function in javascript is not working?
  • I am trying to remove class for specific div tag in HTML with below code.
document.getElementsByClassName("removableClass").removeAttribute('class');

But I got below error. removeAttribute-error

  • Reason behind the error is getElementsByClassName will return us array of Objects
  • So we need to specify which Object to be removed.
  • By providing the index of the specific object or looping through all the objects we can remove the class.
  • If we provide the specific index only that particular index object class will be removed like below.

    	document.getElementsByClassName("removableClass")[0].removeAttribute('class');
  • If we loop through all the Objects we can remove classes from all the Objects as below.

    	for(var i=0;i<document.getElementsByClassName("removableClass").length;i++)
    	document.getElementsByClassName("removableClass")[i].removeAttribute('class');