1

I want to change the appearance of two buttons when either one of them is clicked. But I cannot seem to do that.

I want the classes of the buttons to be changed every time a new button is clicked, but nothing is happening with this code. What could be the problem?

function offbutton() {
    var offbtn = document.getElementById("btndisable3");
    var onbtn = document.getElementById("btnenable3");

    offbtn.classList.remove("btn btn-default");
    offbtn.classList.add("btn btn-danger active");
    onbtn.classList.remove("btn btn-success active");
  onbtn.classList.add("btn btn-default");
}

function onbutton() {
    var offbtn = document.getElementById("btndisable3");
    var onbtn = document.getElementById("btnenable3");

    onbtn.classList.remove("btn btn-default");
    onbtn.classList.add("btn btn-success active");
    offbtn.classList.remove("btn btn-danger active");
    offbtn.classList.add("btn btn-default");
}
<div class="form-group">
  <label class="col-sm-2 control-label">
    <?=$ name ?>
  </label>
  <div class="col-sm-10">
    <div class="btn-group" data-toggle="buttons">
      <label id="btndisable<?= $id ?>" class="btn <?php if($value == 0){ echo 'btn-danger active'; } else{ echo 'btn-default'; } ?>" onclick="offbutton();">
        <div class="col-sm-2">
          <input type="radio" name="options<?= $id ?>" id="disable<?= $id ?>" value="0" <?php if($value==0 ){ echo 'checked'; } ?>> O
        </div>
      </label>
      <label id="btnenable<?= $id ?>" class="btn <?php if($value == 1){ echo 'btn-success active'; } else{ echo 'btn-default'; } ?>" onclick="onbutton();">
        <div class="col-sm-2">
          <input type="radio" name="options<?= $id ?>" id="enable<?= $id ?>" value="1" <?php if($value=='1' ){ echo 'checked'; } ?>> I
        </div>
      </label>
    </div>
  </div>
</div>
4
  • Can you add this to a fiddle? Commented Jun 16, 2016 at 14:07
  • Do you mean switch button? Commented Jun 16, 2016 at 14:08
  • What is a switch button? Commented Jun 16, 2016 at 14:14
  • next time read the console Error: { "message": "Uncaught InvalidCharacterError: Failed to execute 'remove' on 'DOMTokenList': The token provided ('btn btn-default') contains HTML space characters, which are not valid in tokens."... } Commented Jun 16, 2016 at 14:19

1 Answer 1

4

You have to use comma , separator if you want to add/remove several classes so your code should be :

function offbutton() {
    var offbtn = document.getElementById("btndisable3");
    var onbtn = document.getElementById("btnenable3");

    offbtn.classList.remove("btn","btn-default");
    offbtn.classList.add("btn","btn-danger","active");
    onbtn.classList.remove("btn","btn-success","active");
    onbtn.classList.add("btn","btn-default");
}

function onbutton() {
    var offbtn = document.getElementById("btndisable3");
    var onbtn = document.getElementById("btnenable3");

    onbtn.classList.remove("btn","btn-default");
    onbtn.classList.add("btn","btn-success","active");
    offbtn.classList.remove("btn","btn-danger","active");
    offbtn.classList.add("btn","btn-default");
}

Hope this helps.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.