1
$('#btn2').toggle(
    function () {
        $('#btn2').text('show');
    },
    function () {
        $('#btn2').text('hide');                
    }
)

This does not work on my computer (on any browser). Could this be due to a change in the method between jQuery versions?

1
  • 2
    If you want to check if something is (still) supported, you check the documentation. It has very good detail about appropriate usage, as well as the version in which features were added/removed. That should always be your first stop, and always do that before asking a question SO. Commented Feb 14, 2013 at 6:38

4 Answers 4

1

See fiddle

This functionality of .toggle was removed in jQuery 1.9

So this wont work for jQuery 1.9 or later..

See one alternative here

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

Comments

1

Per your updated feature request, to simply alternate between two inner text values:

$('#btn2').click(function() {
    $(this).text( $(this).text()=='show'? 'hide':'show' );
});


That is not one of the signatures of toggle. To use its hide/show capabilities, do this $(elem).toggle() (and obviously if the button is hidden it can't say 'show', so presumably you're toggling something else):

$(elem).toggle();
$('#btn2').text( $(elem).is(':visible') ? 'hide' : 'show' );

2 Comments

sorry but I just want the text (value) on the button change from 'show' to 'hide', 'hide' to 'show'…. once it is clicked. again and again
@user2049259 Note: if you use <button></button>, use text, but if you use <input type='button' /> use val. Working demo: jsfiddle.net/TxXVB
1

Try this: http://jsfiddle.net/W9JCR/

$('#btn2').click(function () {
   ($(this).text()=='show') ? $(this).text('hide') : $(this).text('show');
});

Comments

0

Yes, it's still supported. I think the problem with your code is that you've used text(). Use val()

$('#btn2').toggle(function () {
    $('#btn2').val('show');
    },function () {
        $('#btn2').val('hide');                
});

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.