Reputation: 891
I have a jQuery slideToggle that I am using to display and hide certain content. It always show an arrow pointing down, but if clicked and the text is shown, the arrow should be pointing up. Anyway to do this?
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").slideToggle();
});
});
</script>
<button>↓</button>
<p>Wow!</p>
</body>
</html>
Upvotes: 9
Views: 3338
Reputation: 197
A suggestion based on @Jorrex's answer above.
You can also use e.g. button's class instead of a variable:
$("button").click(function() {
if (!$(this).hasClass('toggled')) {
$(this).html("↑");
} else {
$(this).html("↓");
}
$(this).toggleClass('toggled');
$("p").slideToggle();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>
↓
</button>
<p>
Wow!
</p>
This way it would be much easier if one needs more than one such button on the same page.
Edit: Even if you don't plan to use more than one such button on a page, it's still safer not to use a variable here, because any other function inside $(document).ready(function(){
could possibly interfere with that variable -- and make you waste your time trying to figure out why "that strange things" (see this example) are happening.
(While the error may seem obvious in that example, such errors become much harder to spot when your codebase grows.)
Upvotes: 0
Reputation: 16103
My answer is based on Jorrex's answer. I'm a fan of making code short and readable. Simple logic should be simple code. IMO this next example is easier to read, especially if you have lots of code in your file.
Also, you can just as easily use plain Javascript for this, making it more lightweight.
var toggled = false;
$("button").click(function() {
this.innerHTML = !toggled ? "↑" : "↓";
toggled = !toggled; // flip the value
$("p").slideToggle();
});
Upvotes: 0
Reputation: 27192
Try this it will work :
change the html content
of button on slideToggle()
.
Html :
<button id="up">↑</button>
<p>Wow!</p>
JQuery :
$(document).ready(function(){
$("button").click(function(){
var arrowId = $(this).attr('id');
if (arrowId == 'up') {
$("button").html('↓');
$(this).attr('id','down');
} else {
$("button").html('↑');
$(this).attr('id','up');
}
$("p").slideToggle();
});
});
Demo : https://jsfiddle.net/64a6fafh/1/
Upvotes: 1
Reputation: 1500
As simple as that!
var toggled = false;
$("button").click(function() {
if (!toggled) {
$(this).html("↑");
toggled = true;
} else {
$(this).html("↓");
toggled = false;
}
$("p").slideToggle();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>
↓
</button>
<p>
Wow!
</p>
Upvotes: 6
Reputation: 2995
Use $("button").text('Button Clicked');
to change the text of your button onclick
$(document).ready(function(){
$("button").click(function(){
$("p").slideToggle();
$("button").text('Button Clicked');
});
});
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<button>↓</button>
<p>Wow!</p>
</body>
</html>
Upvotes: 1
Reputation: 6264
Working Demo
$(document).ready(function() {
$("button").click(function() {
$("p").slideToggle();
$(this).toggleClass('down up');
});
});
.down {
background-image: url("https://cdn3.iconfinder.com/data/icons/google-material-design-icons/48/ic_keyboard_arrow_down_48px-128.png");
height: 130px;
width: 130px;
}
.up {
background-image: url("https://cdn3.iconfinder.com/data/icons/google-material-design-icons/48/ic_keyboard_arrow_up_48px-128.png");
height: 130px;
width: 130px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="down"></button>
<p>Wow!</p>
Upvotes: 3
Reputation: 6637
This solutions checks if the button has an arrow pointing 'UP', if it does, replaces it with 'DOWN', and vice versa.
$(document).ready(function(){
$("button").click(function(){
if ($(this).text() == 'up'){
$(this).html('↓')
}else{
$(this).html('up');
}
$("p").slideToggle();
});
});
https://jsfiddle.net/gogaa33d/
Upvotes: 2
Reputation: 76
Try something like this.. :)
$(document).ready(function(){
$("button").click(function(){
$("p").slideToggle();
$( this ).text('NewButtonText');
});
});
Upvotes: 2