0

I have event when I click the text div1 for example will be bold, and remove bold from the other selected, I tried with addClass but it did not work. How can I resolve this? jsfiddle

HTML:

<div id='1_1' ng-click="addClassToSelected($event)">div1</div>
<div id='1_2' ng-click="addClassToSelected($event)">div1</div>
<div id='1_3' ng-click="addClassToSelected($event)">div1</div>
<div id='1_4' ng-click="addClassToSelected($event)">div1</div>
<div id='1_5' ng-click="addClassToSelected($event)">div1</div>
<div id='1_6' ng-click="addClassToSelected($event)">div1</div>
<div id='1_7' ng-click="addClassToSelected($event)">div1</div>

Angular:

$scope.addClassSelected = function(event){
            var id =event.target.id ;
            $('#'+id).addClass("bold-selected");
        };

CSS:

.bold-selected {
    font-weight: bold;
}
kenza
  • 195
  • 1
  • 4
  • 16

2 Answers2

0

You really should be reading up on how to get away from jquery when working with angular. Start at this topic: "Thinking in AngularJS" if I have a jQuery background?

Here is the answer to your question though. First off your dom should look like this if it's going to be something repeated:

<div 
    data-ng-class="{
        'bold-selected': option.id == selectedId
       }"
    data-ng-repeat="option in data" 
    data-ng-click="addClassToSelected(option.id)"
    id="1_{{ data.id }}">
    div1
</div>

Second, this is what your controller would look like instead:

function MyCtrl($scope) {
    $scope.data = [{ id: 1 },{ id: 2 },{ id: 3 },{ id: 4 },{ id: 5 },{ id: 6 },{ id: 7 }];
    $scope.selectedId = false;
    $scope.addClassToSelected = function(id){
        $scope.selectedId = id;
    }
}

Here's the jsFiddle of the final version: http://jsfiddle.net/w2hfourg/

Community
  • 1
  • 1
Mathew Berg
  • 28,625
  • 11
  • 69
  • 90
0

jsfiddle the answer

<span id='1' ng-class='{"myclass":tog==1}' ng-click='tog=1'>span 1</span>
<span id='2' ng-class='{"myclass":tog==2}' ng-click='tog=2'>span 2</span>
kenza
  • 195
  • 1
  • 4
  • 16