user2828924
user2828924

Reputation: 3

JavaScript getElementById(array).style.borderColor not working

I want to change the color of a specific border of an object IN THE ARRAY, but I don't know why it won't change.

var x = [document.getElementById("SelectNro"), document.getElementById("SelectSubtype") ];     
document.getElementById(x[0]).style.borderColor="#FF0000";

Upvotes: 0

Views: 705

Answers (1)

Felix Kling
Felix Kling

Reputation: 816462

x is already an array of elements, so you just need to do

x[0].style.borderColor = "#FF0000";

I don't know why it won't change

The argument to getElementById has to be a string. Any argument you pass to it will be converted to a string implicitly. Converting a DOM element to a string results in something like "[object HTMLDivElement]", i.e. document.getElementById(x[0]) would look for an element with ID [object HTMLDivElement] (which most likely doesn't exist).

Upvotes: 4

Related Questions