jeudi 15 novembre 2018

Array index value on clicking a checkbox - JavaScript

I have JSON array which has 2 check boxes. On Clicking a checkbox i need to get array index of clicked checkbox. Any help would be appreciated

data.json

"contents" : [
    {
        "title" : "Book1",
        "type" : "CHECKBOX"
    },
    {
        "title" : "Book2",
        "type" : "CHECKBOX"
    }
]

templates.json

{
"checkboxTemplate": "<div style='width:15px'><input type='checkbox' id='myCheck' onClick='myFunction()'></div>"
}

In Controller data.json is read and stored it in datalist array.

    function myFunction() {

      var checkBox = document.getElementById("myCheck");
      for (var i = 0; i < datalist.length; i++) {
            if (checkBox.checked == true){
                alert("Checked");
             }
                else
                {
                    alert("Unchecked");

                }
            }
      }

HTML

<div id="container" ng-controller="ContentCtrll as ctrll">
    <content-item ng-repeat="item in ctrll.content" content="item">
    </content-item>
</div>




Aucun commentaire:

Enregistrer un commentaire