Here's an alternative approach that utilizes jQuery:
HTML
<button type="button" class="btn" id="btn1" onClick="toggleActive( this )">Details1</button>
<button type="button" class="btn" id="btn2" onClick="toggleActive( this )">Details2</button>
<button type="button" class="btn" id="btn3" onClick="toggleActive( this )">Details3</button>
<button type="button" class="btn" id="btn4" onClick="toggleActive( this )">Details4</button>
JS
function toggleActive( element ){
deactivateAll();
$( element ).addClass( 'active' );
}
function deactivateAll(){
$( '.btn' ).removeClass( 'active' );
}
Check out the demo here.
Additionally, here's a solution using AngularJS:
JS
angularApp.controller( 'ButtonController', function( $scope ){
$scope.buttons = [
{ id: 'btn1', value: 'Details1' },
{ id: 'btn2', value: 'Details2' },
{ id: 'btn3', value: 'Details3' },
{ id: 'btn4', value: 'Details4' }
]
$scope.activeBtn = undefined;
$scope.toggleActive = function( str ){
$scope.activeBtn = str;
}
});
HTML
<div ng-controller="ButtonController">
<button ng-repeat="button in buttons" ng-class="{ 'active' : activeBtn === button.id }"
ng-click="toggleActive( button.id )" type="button" class="btn" id="{{button.id}}">
{{button.value}}
</button>
</div>
View the AngularJS implementation here.