Trying to get background color of all buttons | Sololearn: Learn to code for FREE!
+ 6

Trying to get background color of all buttons

Iam trying to get the background-color of the button. var btns = document.getElementsByTagName('button'); hoverBtn = false; var bubbleColor; for(i=0;i<btns.length;i++) { button = btns[i]; console.log(btns[i]); btns[i].addEventListener('mouseenter', function() { console.log(button); hoverBtn = true; bubbleColor = button.style.backgroundColor; console.log(button.style.backgroundColor); });

26th Jan 2019, 4:51 PM
Arne Van Kerckvoorde
Arne Van Kerckvoorde - avatar
4 Answers
+ 4
Hi James, I'am trying to get the background color of the button and put it inside the variable bubbleColor but the code returns an empty string.
27th Jan 2019, 9:23 AM
Arne Van Kerckvoorde
Arne Van Kerckvoorde - avatar
+ 2
What do you mean by "get"? I don't see any errors that will break your code (though some of it could be improved). Does it work? What exactly do you want us to help with?
26th Jan 2019, 5:24 PM
James
James - avatar
+ 1
You can't get values from CSS like that, unless you have used JS to assign them. You can assign CSS values like that, though. You need to use something like this instead: getComputedStyle(button).getPropertyValue("background-color"); Have a look this method in action here: https://code.sololearn.com/WtkmtOY8BZ9m/?ref=app
27th Jan 2019, 9:31 AM
James
James - avatar
+ 1
27th Jan 2019, 9:36 AM
James
James - avatar