1

Is there a way to change the link text on the fly with jQuery, if the link class changes?

Initial state (no class):

<a href="#" class="">Save This</a>

If above is clicked it is given a class as below:

<a href="#" class="saved">Save This</a>

I would like to end up with this:

<a href="#" class="saved">Remove This</a>

What I would like if is the class changes to "saved", I want to change the initial text 'Save This' to 'Remove This'.

2
  • 2
    you need to do that when you are adding the class saved. Commented Aug 26, 2014 at 13:03
  • See my answer for your question :) Commented Aug 26, 2014 at 13:21

4 Answers 4

3

Are you trying to do something like this?

http://jsfiddle.net/a6vjxy6c/2/

$("a").on("click", function(e) {
    e.preventDefault();
    if ($(this).hasClass("saved")) {
     $(this).removeClass("saved");
        $(this).html("Save This");
    }
    else {
     $(this).addClass("saved");
        $(this).html("Remove This");
    }

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

Comments

0
$('a').click(function(){
    $(this).addClass('saved');
    $(this).text('Remove This');
});

Fiddle Demo

Comments

0

Try this:

DEMO

Assigning id will provide unique operation

HTML:

<a id="btn" href="#" class="">Save This</a>

SCRIPT:

 $("#btn").click(function () {
            $(this).addClass("saved");
            $(this).html("Remove This");
        });

Comments

0

Try this:

$('a').click(function(e) {
        e.preventDefault();
        $(this).hasClass('saved')?$(this).html('Remove This'):$(this).addClass('saved');
    });

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.