javascript - What's the simplest way to change the color of a div by clicking it? -


i'm trying change background color of div when clicked. code i've written:

<div style="background-color:red" onclick="this.style='background-color:blue'"></div> 

for reason, hasn't been working. i'm sure it's syntax problem. can help?

thanks!

the background color can referred dom property : this.style['background-color']

<div style="background-color:red" onclick="this.style['background-color']='blue'">demo text</div> 

or js property : this.style.backgroundcolor

<div style="background-color:red" onclick="this.style.backgroundcolor='blue'">demo text</div> 

the later more popular. browser ensures both dom property & js property in sync.

check demo :


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -