Home > database >  how to clear a variable's value in javascript
how to clear a variable's value in javascript

Time:12-13

After clicking print button a receipt is shown with a set of values in a modal using javascript. After closing the screen while click again print button a receipt is shown in the modal with previous values.

I want to reset that variable values when the modal is closed.

modal

 <div  id="printModal">
  <div >
    <div >
      <div  id="print-modal">        
        <div id="invoice-POS">                                
            <center id="top">
              <div >
                <img src="{{URL::asset('assets/img/logo/logo.jpg')}}" alt="Wooden Oven" width="80px" height="60px">
              </div>
            </center><!--End InvoiceTop-->            
            <div id="mid">
                <div >
                    <h1>Order : <span id="orders_id"></span></h1>
                    <p>
                        Branch : <span  id="branch_name"></span></br>
                    </p>
                </div>

                <div id="bot">
                    <div id="table">

                    </div>
                </div>
            </div><!--End Invoice Mid-->
        </div><!--End Invoice-->      
      </div>
      <div >
        <button type="button" >print</button>
        <button type="button"  data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

javascript

table = '<table>';
table  = '<tr ><td ><p><b>Item</b></p></td><td ><p><b>Qty</b></p></td><td ><p><b>Rate</b></p></td><td ><p><b>Total</b></p></td></tr>';
table  = '<tr > <td  colspan="3"><p><b>Grand Total :</b></p></td><td ><p>' data['orders'][0].total_amount '</p></td></tr>';
table  = '<tr > <td  colspan="3"><p><b>Discount :</b></p></td><td ><p>' data['orders'][0].discount '</p></td></tr>';
table  = '<tr > <td  colspan="3"><p><b>Net Receivable Amount :</b></p></td><td ><p>' data['orders'][0].net_receivable_amount '</p></td></tr>';
table  = '<tr > <td  colspan="3"><p><b>Paid Amount :</b></p></td><td ><p>' data['orders_paid_amount'][0].paid_amount '</p></td></tr>';
table  = '</table>';
$('#table').append(table);
delete table;
$('#printModal').modal('show');

I have used delete table. But it's not working.

Anybody help please? Thanks in advance

CodePudding user response:

You can try resetting the table value using the table = '';

CodePudding user response:

You can't delete a variable if you have initialized it's value from "var" keyword. But in this case delete would work :

x = 20;
delete x;
console.log(x);

So, if you've used var keyword before table variable then just remove that keyword. It would work fine. Also you can do

table="";

to reset it's value.

CodePudding user response:

You appended your html, so it's too late, you need to remove the html from '#table', like so :

$('#html').empty()

and you need to do it before appending again

  • Related