Reputation: 91
how are you guys?! well I have a question. I have a link on top of a div called "section2" what I need is to change the Div background color on hover and keep that new color until you hover on link number two, background color of that div should change. I don't need to change the background color of the Link, I just need to change dinamically the background color of the div from behind.
<script language="javascript">
$(function(){
$("#changer").on("click",function(e){
e.preventDefault();
var body = document.getElementById("section2"),
green = "rgb(0, 128, 0)",
white = "rgb(255, 255, 255)";
if(body.css("backgroundColor") !== green){
body.css("backgroundColor",green);
}else{
body.css("backgroundColor",white);
}
});
})
</script>
and the html
<div id="section2" align="center">
<a href="#" id="changer">Click me</a>
</div>
Upvotes: 1
Views: 295
Reputation: 23
Following approached worked,
$(function() {
$("#changer").on("click",function(e){
e.preventDefault();
var body = $("#section2");
if(body.css("background-color").indexOf("rgb(0, 128, 0)") == -1 ) {
body.css("background-color","rgb(0, 128, 0)");
}else{
body.css("background-color","rgb(255, 255, 255)");
}
});
})
Upvotes: 0
Reputation: 185
Try to control $("#section2")
background-color property using CSS classes:
#section2.green {
background-color: green;
}
#section2.white {
background-color: white;
}
So, using jQuery to add background-color:
$(function () {
$("#changer").on("click", function (e) {
e.preventDefault();
var body = $("#section2");
if (body.hasClass('green')) {
body.removeClass('green')
.addClass('white');
} else {
body.removeClass('white')
.addClass('green');
}
});
})
Upvotes: 0
Reputation: 193261
Native HTML elements don't have css
method, this is jQuery one. So your code for body
should be:
var body = $("#section2"),
Entire demo:
$(function () {
$("#changer").on("click", function (e) {
e.preventDefault();
var body = $("#section2"),
green = "rgb(0, 128, 0)",
white = "rgb(255, 255, 255)";
if (body.css("backgroundColor") !== green) {
body.css("backgroundColor", green);
} else {
body.css("backgroundColor", white);
}
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="section2" align="center">
<a href="#" id="changer">Click me</a>
</div>
However comparing CSS property value for background color is very unreliable. You would better compare/check classes:
$("#changer").on("click", function (e) {
e.preventDefault();
$("#section2").toggleClass('green white');
});
.green {background-color: rgb(0, 128, 0);}
.white {background-color: rgb(255, 255, 255);}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="section2" class="white" align="center">
<a href="#" id="changer">Click me</a>
</div>
Upvotes: 2
Reputation: 8366
This is how I did it:
$(function () {
$('#changer').hover(function () {
$('#section2').css('background-color', 'yellow');
});
$('#reset').hover(function () {
$('#section2').css('background-color', 'red');
});
})
Upvotes: 0
Reputation: 849
Reference the items using JQuery as so:
$(function () {
$("#changer").on("click", function (e) {
e.preventDefault();
var body = $("#section2"),
green = "rgb(0, 128, 0)",
white = "rgb(255, 255, 255)";
if (body.css("backgroundColor") !== green) {
body.css("backgroundColor", green);
} else {
body.css("backgroundColor", white);
}
});
})
Upvotes: 0