Changing background color through JavaScript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Changing background color through JavaScript

How would I do this? I’m looking to make a option drop-down that will change the background color depending on the option chosen.

13th Nov 2018, 1:41 AM
Yosharu
Yosharu - avatar
2 Answers
+ 6
Basically add a change event listener to the top level element(in this case select tag) then if it matches the clicked element change the background color to that one. select.addEventListener('change', function(e){ if(e.target && e.target.nodeName =="SELECT"){ body.style.backgroundColor = e.target.value } }) //demo code https://code.sololearn.com/W7Fta4ObsASg/?ref=app
13th Nov 2018, 3:19 AM
Lord Krishna
Lord Krishna - avatar
+ 3
You can write: document.style.backgroundColor= '#ABCDEF'; You can see this code it has something related to your query https://code.sololearn.com/WCHCllYIH6KR/?ref=app
13th Nov 2018, 3:03 AM
Шащи Ранжан
Шащи Ранжан - avatar