-1

I gave my elements a class. I then run in javascript

document.getElementsByClassName("class").style.padding = "20px" 

but it didin't work. Is the problem that in javascript padding doesn't work with classes?

Victory
  • 5,811
  • 2
  • 26
  • 45

1 Answers1

2

getElementsByClassName returns an array-like object, so you either need to iterate over all of them in a loop, or specify the individual elements you want to change. It works when you change to an ID because ID's must be unique.

Ex:

var elems = document.getElementsByClassName('class');
Array.prototype.filter.call(elems, function(testElement){
    testElement.style.padding = "80px" 
});

jsFiddle example

j08691
  • 204,283
  • 31
  • 260
  • 272