Obsivus
Obsivus

Reputation: 8359

Click on button changes ID value on the button, click on the new ID changes back to original ID with Jquery

I am trying to use a button as a switch. If I click on the button the value and id changes. If I click on the button again it goes back to original value and id.

Original values could look like this:

value="Show all" id="showall"

Changed to this values

value="Standard" id="default"
<script>
    $(function () {
        $("#showall").click(function () {
            $("#showall") //  change the Value text to Standard on the button
            $("#showall") // Change ID value to default on the button
        });
        $("#default").click(function () {
            $("#default") // change value back to the original which is "Show all"
            $("#default") // change ID back to original which is "Showall"
        });
</script>

Upvotes: 0

Views: 211

Answers (6)

jbabey
jbabey

Reputation: 46657

You should be using a class toggle instead of changing IDs.

http://jsfiddle.net/JzMnM/

if (that.hasClass('foo')) {
    that.removeClass('foo').addClass('bar').val('bar');
} else {
    that.removeClass('bar').addClass('foo').val('foo');
}

Upvotes: 0

Sampson
Sampson

Reputation: 268492

Assuming this button is within some type of <div class="container"></div> where we can handle its event (You could handle it from the body, or document as well, though that's not advised):

$(".container").on("click", "#showall, #default", function(){
  var props = this.id === "showall" 
      ? { value:'Standard', id:'default' } 
      : { value:'Show All', id:'showall' };
  $(this).attr( props );
});

Demo: http://jsbin.com/eravoq/2/edit

Upvotes: 1

fguillen
fguillen

Reputation: 38888

I know is not exactly what you are asking but if we go an step back in your question I think what you really want is to toggle between two buttons so I think is very much maintainable, understandable, intuitive and a lot of more things to just having two well formed buttons and show/hide then alternatively:

<style>
  button#default{
    display: none;
  }
</style>
 ​
<button id="showall" value="Show all">Show all</button>
<button id="default" value="Standard">Standard</button>

<script>
  function toggleButtons(){
    $("button#showall, button#default").toggle();
  }

  $("button#showall, button#default").click( toggleButtons );
</script>

Check the jsFiddle

Upvotes: 0

adeneo
adeneo

Reputation: 318362

$("#showall").on('click', function() {
    this.id = this.id=='showall' ? 'default' : 'showall';
    this.value = this.value=='Standard' ? 'Show All' : 'Standard';
});

FIDDLE

Upvotes: 0

jbduzan
jbduzan

Reputation: 1126

why would you change the ID of the button ? You could do something like that :

  $(function () {
       $("#switchButton").click(function(){
           if($(this).value == "default"){
                $(this).value = "Show all";
                // do other stuff
           }else if($(this).value == "Show all"){
                $(this).value = "default";
                // do other stuff
           }
       });
    });

Upvotes: 0

Thulasiram
Thulasiram

Reputation: 8552

   $("#showall").on('click', function () {
        $(this).attr({ 'value': 'Standard', 'id': 'default' });
    });

    $("#default").on('click', function () {
        $(this).attr({ 'value': 'Show all', 'id': 'showall' });
    });

Upvotes: 0

Related Questions