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
Post a Comment