1

How can I change the text corresponding to a checkbox when it is selected. I want to change the text to one value when selected and reverted back when the box us unselected.

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" >
<div class="container">
  <div class="row">
    <div class="btn-group" data-toggle="buttons">
  <label class="btn btn-primary ck">
    <input type="checkbox"> off
  </label>
  <label class="btn btn-primary ck">
    <input type="checkbox"> off
  </label>
  <label class="btn btn-primary ck">
    <input type="checkbox"> off
  </label>
</div>
  </div>
</div>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script>
  $('input[type=checkbox]:checked').on('change', function () {
    $(this).text("on");
  });
</script>

5 Answers 5

1

If you put the "off" in a span:

<label class="btn btn-primary ck">
  <input type="checkbox"> <span>off</span>
</label>

you can do this:

 $('input[type=checkbox]').on('change', function() {
    var status = $(this).parent().find('span');
    if (status.text() === "on") {
      status.text("off");
    } else {
      status.text("on");
    }
 });

Check out this fiddle: https://jsfiddle.net/wietsedevries/bahujj8u/1/

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

Comments

0

Two things

1) Attaching events to all checkboxes (not only initially checked)

NO: $('input[type=checkbox]:checked')

YES: $('input[type=checkbox]')

2) Changing text of the text, not changing text of the...

NO: $(this).text("on");

YES: $(this).closest("label").text("on");

finally) toogle "on" / "off" text on the label, should be easy :)

Comments

0

Rearrange your DOM -

<div class="container">
  <div class="row">
    <div class="btn-group" data-toggle="buttons">
     <label class="btn btn-primary ck">off </label>
     <input type="checkbox"> 
     <label class="btn btn-primary ck">off</label>
     <input type="checkbox"> 
     <label class="btn btn-primary ck">off</label>
     <input type="checkbox">    
    </div>
  </div>
</div>

Try this jQuery

$('input[type=checkbox]').on('change', function () {

   if ($(this).prev().text() === 'off') {
    $(this).prev().text('on')
    } else {
    $(this).prev().text('off')
    }
});

Here's a fiddle http://jsfiddle.net/hMS6Y/265/

Comments

0

CSS only option :- You could do this with altering the label text and use CSS only - and toggle the display state of the on state text of the label and the off state text:

.btn .off,.btn.active .on {    			
	display:block			
}
.btn.active .off,.btn .on  {				
	display:none			
}
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">


<div class="btn-group" data-toggle="buttons">
	 <label class="btn btn-success active">
	    <input type="checkbox" autocomplete="off" checked>
			<span class='on'>On</span>
            <span class='off'>Off</span>
	</label>
  </div

Comments

0

You could go for a javascript free solution (which is always a plus imo), by using the "next sibling" ~ in combination with the :checked selector that can react to the state of your checkbox.

If you add both "on" and "off" lables in your markup, each with their own class, so it looks something like this:

  <label class="btn btn-primary ck">
    <input type="checkbox"> 
    <span class="label-off">off</span>
    <span class="label-on">on</span>
  </label>

Now you can determine which one is visible depending on the state of the checkbox like this:

.label-on {
  display: none;
}
input:checked ~ .label-off {
  display: none;
}
input:checked ~ .label-on {
  display: inline;
}

Have a look at the example I set up: https://jsfiddle.net/a41d85gb/

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.