Skip to content
Advertisement

how to change font/background colors of a selected option permanently with pure javascript

I require the selected option’s font/background colors change permanently to for example red/yellow after user completed the selection.

Constraints:

  • Code must work in IE11.
  • No jquery. Pure js.
  • Form has id. Selects, options do not have id.
  • Form has tens of selects.
  • Solution does not have to be inline js.
  • If it is a must for solution, I can add class to selects and/or options.

As an example assume “health” was selected from below. So “health” should be seen red/yellow after selection completed and clicked to a space in the page

I don’t know js. I tried as below and did not work in ie11. Browser js is enabled. I couldn’t find an existing Q/A proper for me.

JavaScript
  • why my trial did not work?
  • how to make it work while satisfying my constraints?

here is the jsbin link: https://jsbin.com/hubigarofa/1/edit?html,output

thanks, regards

Advertisement

Answer

Make a seperate function and on call pass the element to that function using this

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement