1

I have a button ("#btnchangepass"), the value of the button is "show". If i click the button the value must change in "hide", if I click the button another time the value must change as it was at the beginning in "show".

I wrote this code, but i don't know how I can get back the beginning value of the button. Can someone give me a hand?

The button is not hide, it's just the value of the button, the name displayed on the button is hide and show.

$('#btnshowhide').click(function(){
    $("#btnshowhide").prop('value', 'hide');
});
4
  • You can't click an hidden element, so if you hide it you need to click another visible item to show it again. Commented Aug 13, 2015 at 8:41
  • @Michelem op want to change the value of button, not show and hide. Commented Aug 13, 2015 at 8:42
  • oh my bad I didn't understand that Commented Aug 13, 2015 at 8:43
  • The button is not hide, it's just the value of the button, the name displayed on the button is hide and show Commented Aug 13, 2015 at 8:44

6 Answers 6

1

You can check for its state and change the value accordingly:

$('#btnshowhide').click(function () {
    if ($("#btnshowhide").val() == 'show') {
        $("#btnshowhide").prop('value', 'hide');
    } else {
        $("#btnshowhide").prop('value', 'show');
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" id="btnshowhide" value="show" />

Fiddle

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

2 Comments

Yess, you are the only one that really understood the question, thank you very much! :)
I don't think I was the only one but hey, glad I could help
1

If you are using jQuery < 1.8 use:

$('#btnshowhide').toggle(function () {
    $("#btnshowhide").attr('value', 'hide');
}, function () {
    $("#btnshowhide").attr('value', 'show');
});

Else, if you just wanna hide the button and show, use ternary operators:

$('#btnshowhide').click(function () {
    $(this).attr('value', $(this).attr('value') == 'hide' ? 'show' : 'hide');;
});

Use .attr() instead of .prop(), as the latter is used for mostly boolean stuff.

Comments

1

$('#btnshowhide').click(function() {
  $(this).val($(this).val() === 'show' ? 'hide' : 'show');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="button" id="btnshowhide" value="show" />

Comments

0

Use the following

$('#btnshowhide').click(function() {
  $(this).prop('value', $(this).prop('value') == 'hide' ? 'show' : 'hide');
  alert('This has val ' + $(this).prop('value'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btnshowhide">button</button>

Comments

0

I would suggest to use data attribute:

$('#btnshowhide').click(function(){
    var val = $(this).val();
    var newVal = "";

   if(val === "hide") {
      newVal = $(this).data("show-text");
   } else {
     newVal = $(this).data("hide-text");
   }

   $(this).val(newVal);
});

Comments

0

Just use hide()

$('#btnshowhide').click(function(){
    $(this).hide();
});

UPDATED:

Sorry I didn't understand, so you need something like this:

JSFiddle

HTML:

<input type="submit" value="hide" id="btnshowhide" />

JS:

$(document).ready(function () {
    $('#btnshowhide').click(function () {
        if ($(this).val() === "hide") {
            $(this).val("show");
        } else {
            $(this).val("hide");
        }
    });
});

2 Comments

Changing button value, not hiding.
Thank you for your answer :)

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.