1

In my JS code, I am appending checkbox as per the selection of dropdown element. If I am selecting a element from dropdown, then at the same inline I am creating a checkbox of same name and disabling the element from dropdown because already showing in checkbox.

This is used to render HighCharts. Now I am trying to uncheck / check the Checkbox and trying on-change function to execute one function but system is not calling that function.

Few snippets of code:

var add_bpx = '<label class="check_btn"><input id="box" type="checkbox" ng-model="'+element+'" ng-change="runCheckedBox()" checked name="'+element+'" '+element+'</label>';
$('#input_row').append(add_box);

Output:

<div class="input_row" id="input_row">
  <label class="check_btn">
    <input id="box" type="checkbox" ng-model="Box1" ng-change="runCheckedBox()" checked="" name="Box1">Box1
  </label>
  <label class="check_btn">
    <input id="box" type="checkbox" ng-model="Box2" ng-change="runCheckedBox()" checked="" name="Box2">Box2
  </label>
</div>

AngularJs:-

  $scope.runCheckedBox = function(){
    console.log('RunCheck Box executed'); 
  }

Here function is not calling.

1
  • With checkbox i think is better to use ng-click Commented Mar 24, 2015 at 8:50

2 Answers 2

1

It works fine on a sample fiddle, can you check the controller?

var elements = ['b1', 'b2', 'b3', 'b4'];
for (var i = 0; i < elements.length; i++) {
    var element = elements[i];
    var add_box = '<label class="check_btn"><input id="box" type="checkbox" ng-model="' + element + '" ng-change="runCheckedBox()" checked name="' + element + '" ' + element + '</label>';
    $('#input_row').append(add_box);
}

var app = angular.module('app', []);
app.controller('myCtrl', function ($scope) {
  $scope.runCheckedBox = function(){
    console.log('RunCheck Box executed'); 
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body ng-app="app">
    <div ng-controller="myCtrl">
        <div class="input_row" id="input_row"></div>
    </div>
</body>

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

3 Comments

Hey Huy, Thanks a lot for the reply... I tried your snippet code at Jsfiddle. But it is not working. Please have a look .. May be I missed something. JsfiddleLink : jsfiddle.net/rpatil/q54protv
jsfiddle.net/q54protv/1. In order for angularJS to work in JSFiddle, you have to choose "No wrap - in <body>" on the left select box.
Thanks Huy, I saw the new fiddle link. It is working now. But I am unable to implement this feature inside my html. You said like add NoWrap in body. But I am not able to do that. Please suggest something.
0

First thing I would say is take a look at this:

"Thinking in AngularJS" if I have a jQuery background?

Your problem is that when you add the input, angular needs to compile it. I would suggest rather than adding the checkboxes in your select handler, you add them all to the html up front using ng-repeat, and use a model variable together with ng-show to control which ones are visible.

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.