0

Have a basic link that I am using as a button and changing the text when the user clicks it to go from edit to done editing. When I first click the button, the click event happens but the text does not change until I click it again which is throwing off and not behaving as I would like:

HTML:

 <a type="button" id="editButton" class="editButton" style="cursor: pointer">EDIT</a>

JS:

$("#editButton").click(function () {
    var $this = $(this);
    $this.toggleClass('editButt');
    if ($this.hasClass('editButt')) {
        $this.text('EDIT');
    } else {
        $this.text('DONE EDITING');
    }
});

Fiddle

2
  • 1
    Its working see jsfiddle.net/U8Ns3/5 what make you think its not working Commented Jan 23, 2014 at 20:22
  • I see what you're saying. That certainly makes it react quicker. Now I need to add the class. Thanks. Commented Jan 23, 2014 at 20:23

3 Answers 3

3

You toggle a class that is not present at first click, so it mean it will add it and then run your condition.

Add the class and problem solved

<a type="button" id="editButton" class="editButton editButt" style="cursor: pointer">EDIT</a>

http://jsfiddle.net/U8Ns3/4/

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

1 Comment

That's it Karl-André Gagnon - Thank you. Will mark as the answer when I can in 8 mins.
0

Try this out, and see fiddle

$("#editButton").click(function () {
    var $this = $(this);
    if ($this.hasClass('editButt')) {
        $this.text('EDIT');
    } else {
        $this.text('DONE EDITING');
    }
    $this.toggleClass('editButt');
});

Comments

0

Simpler solution, here's a FIDDLE

<button type="button" id="editButton" class="edit">EDIT</button>


$('#editButton').click(function() {
  var text = ($(this).text() === 'EDIT') ? 'DONE EDITING' : 'EDIT';
  $(this).text(text).toggleClass('done');
});

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.