I'm trying to use jQuery
to change the background color of a <div>
section when pressing on a button, and below is the code to do that. Why isn't it working?
HTML file
<html>
<head>
<script type="text/javascript" src="jquery-1.6.4.js"></script>
<script type="text/javascript" src="script.js"></script>
<style type="text/css">
#name{
background-color: #FFFFF2;
width: 100px;
}
</style>
</head>
<body>
<input type="button" id="bgcolor" value="Change color"/>
<div id="name">
Abder-Rahman
</body>
</html>
script.js
$("bgcolor").click(function(){
$("#name").animate(
{backgroundColor: '#8B008B'},
"fast");}
);
Thanks.
Ensure you include the CSS selector inside the jQuery that you wish to target.
Should be:
Because you are targeting an ID.
From the jQuery website:
Use jquery UI package to animate colors.
You have several problems:
$("#bgcolor")
</div>
tag.background-color
.HTML:
JS:
Your code, fixed (and working with jQueryUI included): http://jsfiddle.net/andrewwhitaker/rAVj9/