-1

How can i change the button text and class like toggle, default it is blue and text show onlick it should change to hide with white button background.

<button class="btn exam-int-btn">Show</button>

jsFiddle here

harry
  • 25
  • 10

8 Answers8

1

Use <span>

<button class="btn exam-int-btn">
<span>Show</span>
<span style="display:none">Hide</span>
</button>

CSS:

.white {background-color:#fff !important;color:#000}

jQuery:

$(document).ready(function() {
    $('button').on('click',function() {
        $(this).toggleClass('white').find('span').toggle();
    });
});

fiddle: https://jsfiddle.net/nop97njv/

Taufik Nur Rahmanda
  • 1,862
  • 2
  • 20
  • 36
0

Do you want something like this below Example ???

$('button').on('click',function(){
  $(this).toggleClass("show hide");
})
.exam-int-btn {
   width: 100px;
    line-height: 2.5em;
    justify-content: center;
    padding: 0;
    font-weight: 400;
    color: #ffffff;
    background-color: #2196F3;
    text-align: center;
    border-radius: 4px;
    box-shadow: 5px 5px 15px 0 rgba(46,61,73,.15);
    transition: all .3s ease;
    border: 1px solid transparent;
    cursor: pointer;
}
.show{
   background-color: #2196F3;
}
.hide{
   background-color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="btn exam-int-btn">Enroll</button>
Maulik
  • 765
  • 3
  • 14
0

Check below fiddle

Are you looking for this?

http://jsfiddle.net/yogesh078/Bnuy7/2174/

    var onClick = function() {
    var btn= document.getElementById('btnEnroll');
    var className = btn.getAttribute("class");
  if(className=="exam-int-btn") {
    btn.className = "white";
  }
  else{
    btn.className = "exam-int-btn";
  }

};

$('#btnEnroll').click(onClick);
Yogesh
  • 655
  • 5
  • 13
0

Button

<button class="show">Show</button>

Javascript / jQuery

 `$( ".show" ).click(function() {
     $( this ).removeClass( "show" ).addClass("hide");
     $(this).html('Hide');
  });

  $( ".hide" ).click(function() {
      $( this ).removeClass( "hide" ).addClass("show");
      $(this).html('Show');
  });`

CSS - Do Your CSS Lines

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Janen R
  • 729
  • 10
  • 21
0

$('button').on('click',function(){
    if(!$(this).hasClass('red')){
       $(this).addClass('red').text('Enrolled');
    }else{
       $(this).removeClass('red').text('Enroll');
    }
 
})
.exam-int-btn {
   width: 100px;
    line-height: 2.5em;
    justify-content: center;
    padding: 0;
    font-weight: 400;
    color: #ffffff;
    background-color: #2196F3;
    text-align: center;
    border-radius: 4px;
    box-shadow: 5px 5px 15px 0 rgba(46,61,73,.15);
    transition: all .3s ease;
    border: 1px solid transparent;
    cursor: pointer;
}
.exam-int-btn.red{
    background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="btn exam-int-btn">Enroll</button>
0

You can do like this using jQuery:

$('#my-btn').click(function(){
  //change text
  $(this).text('changed text here');
   
  //change text & background color
  $(this).css({
      'color' : '#000',
      'background-color' : '#fff'
  });
})
.exam-int-btn {
   width: 100px;
    line-height: 2.5em;
    justify-content: center;
    padding: 0;
    font-weight: 400;
    color: #ffffff;
    background-color: #2196F3;
    text-align: center;
    border-radius: 4px;
    box-shadow: 5px 5px 15px 0 rgba(46,61,73,.15);
    transition: all .3s ease;
    border: 1px solid transparent;
    cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="my-btn" class="btn exam-int-btn">Enroll</button>
Deepansh Sachdeva
  • 1,168
  • 9
  • 16
0

Hope this will helps

$('button').on('click',function(){
 
   $(this).text($(this).text() == "Enroll" ? "Enrolled": "Enroll");
  $(this).toggleClass('btn-toggle')
})
.exam-int-btn {
   width: 100px;
    line-height: 2.5em;
    justify-content: center;
    padding: 0;
    font-weight: 400;
    color: #ffffff;
    background-color: #2196F3;
    text-align: center;
    border-radius: 4px;
    box-shadow: 5px 5px 15px 0 rgba(46,61,73,.15);
    transition: all .3s ease;
    border: 1px solid transparent;
    cursor: pointer;
}

.btn-toggle{
background-color:white;
color:#2196F3;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="btn exam-int-btn">Enroll</button>
Bharath Kumar
  • 548
  • 1
  • 6
  • 18
0

Very easy, this is my solution:

    var flag = false; //default text show, background-color: blue
    $("button").click(function(e){
       e.preventDefault();
      if(flag==true){
        flag=false;
      <!-- change text, background-color -->
        $(this).text("Show");
     $(this).css("background-color","blue");
      }else{
    
     flag=true;
        $(this).text("Hide");
     $(this).css("background-color","white");
    }
    })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button style="background-color:blue">Show</button>
minhung
  • 18
  • 1