I have the JQuery following code in my client app design which I am trying to convert to AngularJS directive as shown below, but for some reason the on click event is not responding (nothing happens when I click on boxes) so can someone help me please by telling me what exactly I am doing wrong and how to fix it? Thanks
Original JQuery code:
$("html").on("click", '.boxes .col .ten', function(e){
e.stopPropagation();
var numBoxes = $('.boxes .col .ten.active').length;
if($(this).hasClass('active'))
{
$(this).removeClass('active');
$(this).find('input[type="checkbox"]').val('0');
return false;
}
else
{
if(numBoxes <10)
{
$(this).addClass('active');
$(this).find('input[type="checkbox"]').val('1');
return false;
}
}
});
Directive:
.directive('BoxesDirective', function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind("click", '.boxes .col .ten', function(e){
e.stopPropagation();
var numBoxes = $('.boxes .col .ten.active').length;
if($(this).hasClass('active'))
{
$(this).removeClass('active');
$(this).find('input[type="checkbox"]').val('0');
return false;
}
else
{
if(numBoxes <10)
{
$(this).addClass('active');
$(this).find('input[type="checkbox"]').val('1');
return false;
}
}
});
}
};
});
HTML:
<div class="labelTarget" boxes-directive>
<div class="boxes">
<div class="col">
<div class="col ten active">
$5
<input id="5" name="5" type="checkbox" value="5">
</div>
<div class="col ten">
$10
<input id="10" name="10" type="checkbox" value="10">
</div>
<div class="col ten">
$20
<input id="20" name="20" type="checkbox" value="20">
</div>
<div class="col ten">
$50
<input id="50" name="50" type="checkbox" value="50">
</div>
<div class="col ten">
$75
<input id="75" name="75" type="checkbox" value="75">
</div>
</div>
<div class="col">
<div class="col ten active">
$100
<input id="100" name="100" type="checkbox" value="100">
</div>
<div class="col ten">
$200
<input id="200" name="200" type="checkbox" value="200">
</div>
<div class="col ten">
$500
<input id="500" name="500" type="checkbox" value="500">
</div>
<div class="col ten">
$750
<input id="750" name="750" type="checkbox" value="750">
</div>
<div class="col ten">
$1000
<input id="1000" name="1000" type="checkbox" value="1000">
</div>
</div>
</div>
</div>
</div>
.directive('boxesDirective', function() {solidBoxesDirectiveshould give the attribute namesolid-boxes-directive. I honestly don't know what attribute name it expects if the first letter is uppercase :)