javascript - Input fields are repeating in preview page -


i have 3 check boxes 2 auto selected , displaying text field label check box selected.

but after clicking on preview getting input field twice. mean getting 4 text field instated of two.

there issue ready script.would me out in this?

<!--if check box clicked active script-->    $(function() {    $(".add_student_input").change(function() {      if (this.checked) {        $(".students_items").append('<div class="form-group row ' + this.id + '"><label class="col-sm-4 col-form-label">' + $(this).next('label').text() + '</label><div class="col-sm-8"><input type="' + $(this).data('type') + '" name="input[]" placeholder="' + $(this).next('label').text() + '" class="form-control" /></div></div>');      } else {        $('.students_items').find('.' + this.id).remove();      }    });    });    <!--end script -->  $(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();              }          });       });        /*form preview*/  function printpreview() {          var toprint = document.getelementbyid('open_in_preview');          var popupwin = window.open('', '_blank');          popupwin.document.open();   popupwin.document.write('<html><title>::print preview::</title><link rel="stylesheet" type="text/css" href="print.css" media="screen"/></head><body>')          popupwin.document.write(toprint.innerhtml);          popupwin.document.write('</body></html>');          popupwin.document.close();      }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>  <body id="open_in_preview">  <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>    <a href="#" class="btn btn-primary" onclick="printpreview();">preview form</a>  <span class="students_items"></span>  </body>

i getting output this.

enter image description here

as comment, why script putting @ below of page cause multiple input, when onload, run document ready , append again based on checkbox, solve issue, put empty() onto "reset" input before append new 1

$(document).ready( function(){          $checkbox=$(".add_student_input");          $(".students_items").empty(); //add clear         $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();              }      });  });  

update question op please @ plnkr https://plnkr.co/edit/wngwwcgaysrjfnqwqx3j?p=preview

the issue here not related empty(), how behave on dom ready(but remove question different now), had enhance script take needed tag, , combine script make clearer, please have look, in order read checked behavior in html, need add

this.setattribute("checked", "checked"); 

this force checked being render in html, , $(function(){}) same $(document).ready(function(){})


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -