0

I want to populate a list of check boxes dynamically using JavaScript. I have done the following:

In my balde view:

<div  id="demo" class="collapse" > 
    <div class="panel panel-default" >
          <div class="panel-body" id="demos" style="max-height: 100px;overflow-y: scroll;" >

             <input type="checkbox" name="specific[]" value=" "> <br>

           </div>    
     </div>
 </div>

My script:

$(document).ready(function(){
     $('#ItemID').on('change',function(e){
        console.log(e);
        var itemID= e.target.value;


        $.getJSON('/xxxxx/list?itemID=' + itemID, function(data){
           $('#demos').empty();
           $.each(data,function(index, subcatlist){
               $('#demos').append('<input value="'+subcatlist[0].ID+'">'+subcatlist[0].Name+'</input>');
            });
          });             
        });
      });

route.php passes wanted data.

I want my view to be like this, a checkbox followed by Names:

Expected result

But what I'm getting is this:

Actual result

I am getting Names to the view, but the leading check box is missing and replaced with a textbox. How do I fix this problem?

1
  • in you append call you are not giving type="checkbox". Can you please try that. Commented Sep 15, 2016 at 8:03

2 Answers 2

2
+50

Try this :

$('#demos').append('<input type="checkbox" value="' + subcatlist[0].ID + '"/>' + subcatlist[0].Name);

I think this will solve the problem.

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

Comments

1

There are couple of things which are wrong.

  1. as I have mentioned in the comment, you have missed type="checkbox".
  2. subcatlist should be data and instead of using using 0 , use index.

so your code should be like below.

$.each(data, function (index, subcatlist) {
        $('#demos').append('<input value="' + data[index].ID + '" type="checkbox">' + data[index].Name + '</input>');
    });

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.