0

JavaScript:

var acc = document.getElementsByClassName("togglebtn"),
    i;
for (i = 0; i < acc.length; i++) {
   acc[i].onclick = function () {
       this.classList.toggle("active");                               
   }
}

html:

<md-button class="md-fab togglebtn"></md-button>

css:

button.togglebtn:after {
   background-color: white;
   width: auto;
   font-weight: bold;
   margin-left: 5px;
   border: none;
   content:'';
}

button.togglebtn.active:after {
   width: auto;
   background-color: green;
   content:'close';
   font-size: 10px;
}

I am only able to change the color of the text but not the background color. I am trying to change the color when someone click on button. The :after is working in css but changing color is not working. Can someone suggest me, what I am doing wrong please

1
  • 1
    As you are using AngularJS, it may be better to do it the Angular way. See my answer. Commented Aug 30, 2017 at 12:47

4 Answers 4

3

var acc = document.getElementsByClassName("togglebtn");
var i;
for (i = 0; i < acc.length; i++) {
  acc[i].onclick = function() {
    this.classList.toggle("active");
  }
}
button.togglebtn.active {
  background-color: green;
  color: #fff;
}
<button class="md-fab togglebtn">hello</button>

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

2 Comments

this is working fine but my button changing the width
Give some fixed width.. It'll fix your problem, Remove width: auto on active
1

Its just a CSS problem ,you are trying to set style of after but you need to set style of button and after separately:

var acc = document.getElementsByClassName("togglebtn"),
  i;
for (i = 0; i < acc.length; i++) {
  acc[i].onclick = function() {
    this.classList.toggle("active");
  }
}
button.togglebtn:after {
  content: '';
}

button.togglebtn.active:after {
  content: 'close';
}

button.togglebtn {
  background-color: white;
  width: auto;
  font-weight: bold;
  margin-left: 5px;
  border: none;
}

button.togglebtn.active {
  width: auto;
  background-color: green;
  content: ' close';
  font-size: 10px;
}
<button class="md-fab togglebtn">hello</button>

2 Comments

thats what i am doing but I am not able to change the color though
No,In addition I've added css for button.togglebtn and button.togglebtn.active
1

As you are using AngularJS, I would suggest you to use ng-class instead of plain JavaScript:

var myApp = angular.module('myApp', []);
.active { background-color: red; }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp">
    <button ng-class="{active: isActive}" ng-click="isActive =! isActive">Hello</button>
</div>

1 Comment

I tried to create ng-class and set it up on ng click but its not functioning correctly. I also have another function on ng-click
0
     .md-button.md-fab {
      line-height: 1% !important;
      min-width: 0;
      border-radius: 50%;
      position: fixed;
      margin-left: 400px;
      background-color: green;
      margin-top: -7px;
      }
      button.togglebtn:after {
        color: white;
        width: auto;
        margin-left: 5px;
        border: none;
        content:'open';
        font-size: 10px;

    }

button.togglebtn.active:after {
    width: auto;
    color: black;
    content:'close';
    font-size: 10px;
}

.md-button.md-fab {
    background-color: black !important;
}
.md-button.md-fab.active{
    background-color: yellow !important;
    content: 'yellow';
    color: white;
}

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.