Reputation: 1567
After seeing Toggling button text in jquery this question, I tried to re create it in my situation but can't seem to have it work.
Here is a fiddle of what I've done: http://jsfiddle.net/V4u5X/
$('.SeeMore2').click(function(){
var $this = $(this);
$this.toggleClass('SeeMore');
if($this.hasClass('.SeeMore2')){
$this.text('See More');
} else {
$this.text('See Less');
}
});
It seems to only ever run the if statement once. What am I missing?
Upvotes: 22
Views: 151406
Reputation: 31
Use :
$("button").click(function(){
$(this).html("the new text");
});
Upvotes: 2
Reputation: 872
In HTML:
<button type="button" id="AddButton" onclick="AddButtonClick()" class="btn btn-success btn-block ">Add</button>
In Jquery write this function:
function AddButtonClick(){
//change text from add to Update
$("#AddButton").text('Update');
}
Upvotes: 3
Reputation: 316
try this, this javascript code to change text all time to click button.http://jsfiddle.net/V4u5X/2/
html code
<button class="SeeMore2">See More</button>
javascript
$('.SeeMore2').click(function(){
var $this = $(this);
$this.toggleClass('SeeMore2');
if($this.hasClass('SeeMore2')){
$this.text('See More');
} else {
$this.text('See Less');
}
});
Upvotes: 7
Reputation: 345
its work short code
$('.SeeMore2').click(function(){
var $this = $(this).toggleClass('SeeMore2');
if($(this).hasClass('SeeMore2'))
{
$(this).text('See More');
} else {
$(this).text('See Less');
}
});
Upvotes: 2
Reputation: 2726
This should work for you:
$('.SeeMore2').click(function(){
var $this = $(this);
$this.toggleClass('SeeMore2');
if($this.hasClass('SeeMore2')){
$this.text('See More');
} else {
$this.text('See Less');
}
});
Upvotes: 1
Reputation: 2299
$('.SeeMore2').click(function(){
var $this = $(this);
$this.toggleClass('SeeMore2');
if($this.hasClass('SeeMore2')){
$this.text('See More');
} else {
$this.text('See Less');
}
});
This should do it. You have to make sure you toggle the correct class and take out the "." from the hasClass
Upvotes: 46