I have this html code:
<a class="btn pull-left" id="btnSelectAll"><i class="icon-plus"></i>
Select All
</a>
When I click the button I need to toggle the text to "Deselect All" and also change the class of the <i>
to "icon-minus", for each click should change between one or the other. I tried with toggle on the anchor but didn't work. I don't want to hardcode the text because it can be in a different language.
Here is what I tried:
$("#btnSelectAll").click(function () {
var $this = $(this);
$this.toggleClass('show');
if ($this.hasClass('show')) {
$this.text('@L.GetLanguageText("DeselectAll")');
$($this +'> i').addClass('icon-minus');
} else {
$this.text('@L.GetLanguageText("SelectAll")');
$($this + '> i').addClass('icon-plus');
}
});
but it doesn't change the icon.
Just missing a few css adjustments, jsfiddle .
js:
var alterIcon = function()
{
$("#btnSelectAll").click(function() {
var icon = $(this).children("i"),
text = $(this).text();
$(this).text(text == "Deselect All" ? "Select All" : "Deselect All");
$(this).prepend(icon.toggleClass('glyphicon-thumbs-up glyphicon-thumbs-down') );
});
}
alterIcon();
Hope this help.
Look at this fiddle: http://jsfiddle.net/YDqZW/
html:
<a class="btn pull-left" id="btnSelectAll"><i class="icon-plus"></i>
Select All
</a>
js:
$('#btnSelectAll').on('click', function() {
var notice = 'Select All';
if ( $('#btnSelectAll').text().trim() == notice ) {
notice = 'Deselect All';
}
$('#btnSelectAll').find('i').toggleClass('icon-plus icon-minus').parent().contents().last()[0].textContent = notice;
});
Try this one for size:
$("#btnSelectAll").on("click", function() {
x = $(this).children()[0];
$(this).text().trim() == "Select All" ? $(this).text("Deselect All").prepend(x) : $(this).text("Select All").prepend(x);
$($(this).children()[0]).toggleClass("icon-plus icon-minus");
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.