Reputation: 125
This is the code i am dealing with,and i want to use both fade in and fade out button but having problem as i don't know what id to pass for second button here is the code with jquery:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button:first").click(function(){
$("#div1").fadeIn();
$("#div2").fadeIn("slow");
$("#div3").fadeIn(3000);
});
$("button:b2").click(function(){
$("#div1").fadeOut();
$("#div2").fadeOut("slow");
$("#div3").fadeOut(3000);
});
});
</script>
</head>
<body>
<p>Demonstrate fadeIn() with different parameters.</p>
<button id="b1">Click to fade in boxes</button><br><br>
<button id="b2">Click to fade out boxes</button><br><br>
<div id="div1" style="width:80px;height:80px;display:none;background-color:red;"></div><br>
<div id="div2" style="width:80px;height:80px;display:none;background-color:green;"></div><br>
<div id="div3" style="width:80px;height:80px;display:none;background-color:blue;"></div>
Upvotes: 1
Views: 54
Reputation: 72289
You need to provide id
of corresponding button to make it work
$("button#b2").click(function() {
Working fiddle:- https://jsfiddle.net/1jtbaeqb/
Note:- In your current code case :last
will also correct
$("button:last").click(function() {
Working fiddle:- https://jsfiddle.net/0ptjf7be/
Note:- fadeToggle() modified version is also applicable:-https://jsfiddle.net/hrt1jr86/ (I know you not needed but for your knowledge sake)
Upvotes: 1
Reputation: 1361
I think this is more straightforward:
$(document).ready(function(){
$("#b1").click(function(){
$("#div1").fadeIn();
$("#div2").fadeIn("slow");
$("#div3").fadeIn(3000);
});
$("#b2").click(function(){
$("#div1").fadeOut();
$("#div2").fadeOut("slow");
$("#div3").fadeOut(3000);
});
});
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<p>Demonstrate fadeIn() with different parameters.</p>
<button id="b1">Click to fade in boxes</button><br><br>
<button id="b2">Click to fade out boxes</button><br><br>
<div id="div1" style="width:80px;height:80px;display:none;background-color:red;"></div><br>
<div id="div2" style="width:80px;height:80px;display:none;background-color:green;"></div><br>
<div id="div3" style="width:80px;height:80px;display:none;background-color:blue;"></div>
Upvotes: 1
Reputation: 864
Replace your 2nd button click function to : -
$("button:last").click(function(){
$("#div1").fadeOut();
$("#div2").fadeOut("slow");
$("#div3").fadeOut(3000);
});
});
Upvotes: 2