22

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?

4
  • 6
    Don't put a . in the argument to hasClass. Commented Oct 14, 2013 at 17:03
  • If I do that it actually doesn't change the text at all Commented Oct 14, 2013 at 17:04
  • 1
    You toggle the seeMore class but never deals with seemore2 Commented Oct 14, 2013 at 17:04
  • 1
    possible duplicate of Button text toggle in jquery Commented Oct 14, 2013 at 17:06

6 Answers 6

46
$('.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

http://jsfiddle.net/V4u5X/2/

Sign up to request clarification or add additional context in comments.

2 Comments

facepalm I misunderstood how the argument worked for .toggleClass. I assumed that you would specify a seperate class within the (). In my original it still has the class of SeeMore2, now that I look at it. Thanks for the help!
Doesn't work on archives which contain multiple entries with more than 1 instance of the same button. @Scherf
7

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');
        }
    });

Comments

3

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');
    }

Comments

2

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'); } });

Comments

2

Use :

$("button").click(function(){
  $(this).html("the new text");
});

Comments

1

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');
        }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.