-2

HTML form has a input element with the default value, while printing the form using java-script it is print the default value even it is changed in DOM

JAVASCRIPT

$(document).on('click','.print',function(){
  var content = $('#printable_div').html();
  document.body.innerHTML =  content;
  window.print();
  document.body.innerHTML = content;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  
<div id='printable_div'>
  <div class='row'>
    <div class='col-md-2'><span>OPT value</span>
      <input type='text' class='form-control' value='50' />
    </div>
  </div>
</div>
<input type='button' class='btn btn-primary print' value='Print'>

Here default value of the input is 50, and i changed this value to 0 and tried to print it, but it's printing with the default value 50

Tomasz Mularczyk
  • 34,501
  • 19
  • 112
  • 166
Ashok Sri
  • 196
  • 2
  • 14

2 Answers2

1

It will be helpful to you, here I have added

var v=$(".in_put").val();$(".in_put").attr("value",v);

      $(document).on('click','.print',function(){
              var v=$(".in_put").val()
              $(".in_put").attr("value",v);
              var content = $('#printable_div').html();
              document.body.innerHTML =  content;
              window.print();
              document.body.innerHTML = content;
          });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='printable_div' >
              <div class='row'>
                    <div class='col-md-2'><span>OPT value</span>
                        <input type='text' class='form-control in_put' value="50"  />
                    </div>
               </div>
    </div>
       <input type='button' class='btn btn-primary print' value='Print'>
Kalaiselvan
  • 2,095
  • 1
  • 18
  • 31
1

You can try like this.

$(document).on('click','.print',function(){
       $('#input-text').val(0);
       window.print();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id='printable_div'>
  <div class='row'>
    <div class='col-md-2'><span>OPT value</span>
        <input type='text' id="input-text" class='form-control' value='50' />
    </div>
   </div>
</div>
<input type='button' class='btn btn-primary print' value='Print'>
Pravin Vavadiya
  • 3,195
  • 1
  • 17
  • 34