2

I am changing the class of a button, of a button group, using javascript within angular directive.

My update is happening correctly. But the change is not reflecting on screen until i click somewhere in the page.

angular.module('test', []).
directive('selected', function() {
  return {
    link: selectedLinker,
    restrict: 'A',
    replace: false
  }

  function selectedLinker(scope, element, attribute) {
    var groupname = attribute.selected;
    groupname = 'groupname';
    element.bind('click', function() {
      var elements = document.querySelectorAll('[selected=' + groupname + ']');
      for(var i=0; i<elements.length; i++) {
        var item = elements[i];
        item.classList.remove('selected');
      }
      this.classList.add('selected');
      //element.addClass('selected');
    })
  }
});

What am I missing here. I dont want to use jquery. Thanks.

Pluker Link

2
  • 1
    nothing to do with angular ... has to do with bootstrap :focus css selector. Inspect element in browser dev tools and look at css rules that apply ... will see the :focus selector as part of same rule for .selected
    – charlietfl
    Commented May 15, 2016 at 4:27
  • Thanks. I got it working.
    – jsbisht
    Commented May 15, 2016 at 4:43

2 Answers 2

1

try adding this to your css :

.btn:focus {
      background-color: inherit !important;
      color: inherit !important;
    }
1

Add this.blur() after this.classList.add('selected'); in your directive's link function to clear the bootstrap CSS properties being applied due to the button being in focus.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.