javascript - Input text type showing undefined in view source -


i have 3 check box 2 auto selected. have display text field label check box selected.

i tried below code displaying 1 text field , type undefined checked in view source. think there issue this. me out?

$(document).ready( function(){       $checkbox=$(".add_student_input").is(":checked");          if($checkbox == true) {              $(".students_items").append('<div class="' + this.id + '"><label>' + $(this).next('label').text() + '</label><input type="' + $(this).data('type') + '" name="input[]" placeholder="' + $(this).next('label').text() + '" class="form-control" /></div>');              } else {                   //check false                    $('.students_items').find('.' + this.id).remove();              }          });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>     <input type="checkbox" name="a"  id="class_1" class="add_student_input" data-type="text"  checked><label for="class_1">number1</label>      <input type="checkbox" name="b"  id="class_2" class="add_student_input" data-type="text" checked><label class="class_2">number2</label>      <input type="checkbox" name="c"  id="class_3" class="add_student_input" data-type="text"><label class="class_3">number3</label>  <span class="students_items"></span>

you missing loop $(".add_student_input") gives 3 checkboxes. so, need use each() loop on 3 checkbox , check checked checkbox , create respective text element it.

$(document).ready( function(){       $checkbox=$(".add_student_input");       $checkbox.each(function(){          var ischecked = $(this).is(":checked");           if(ischecked) {              $(".students_items").append('<div class="' + this.id + '"><label>' + $(this).next('label').text() + '</label><input type="' + $(this).data('type') + '" name="input[]" placeholder="' + $(this).next('label').text() + '" class="form-control" /></div>');              } else {                   //check false                    $('.students_items').find('.' + this.id).remove();              }          });       });         
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>    <input type="checkbox" name="a"  id="class_1" class="add_student_input" data-type="text"  checked><label for="class_1">number1</label>      <input type="checkbox" name="b"  id="class_2" class="add_student_input" data-type="text" checked><label class="class_2">number2</label>      <input type="checkbox" name="c"  id="class_3" class="add_student_input" data-type="text"><label class="class_3">number3</label>            <div class='students_items'></div>


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -