Search code examples
javascripthtmlqlikviewqliksense

how we can get value from nprinting variable using javascript


I'm applying this simple HTML code with javascript in Nprinting - I can't get the value of () to calculate the sum using javascript because Nprinting value (%%aRWgmA%%) will be added after running the task- is there any other solution to calculate the sum

Thanks!

<html lang="en">
     <head></head>
     <body  style="margin:0; padding:4px"> 
<table id="reporttable">
                <tr>
                    <th> Date </th>                 
                    <th> Discount Balance </th>           
                    <th> Promo Discount Balance </th>           
                    <th> Plan Balance </th>           
                    <th> Main Accounts </th>
                    <th> Total </th>         
                </tr>
                <tr>
                    <td> %%Date1 - Customer Balance Snapshot%% </td> 

                   <td class="somme"> %%aRWgmA%% </td>   
                   <td class="somme"> %%cTzY%% </td>     
                   <td class="somme"> %%DYpRdtH%% </td>  
                   <td class="somme"> %%KgXCjza%% </td>  
                   <td class="somme"> %%tjcDbA%% </td>   


                </tr>
         </table>  



  <script language="javascript" type="text/javascript">
            var tds = document.getElementById('reporttable').getElementsByTagName('td');
            var sum = 0;

             for(var i = 0; i < tds.length; i ++) {
                if(tds[i].className == 'somme') {


                    sum += isNaN(tds[i].firstChild.data) ? 0 : parseInt(tds[i].firstChild.data);
                }
            }
            document.getElementById('reporttable').innerHTML += '<tr><td>total</td><td>' + sum + '</td></tr>';
</script>              
     </body>
</html>

Solution

  • You have to wait for the DOM to load the page and then run the JS. The easiest solution is to use jquery (I find this is always working for me):

    $(document).ready(function(){ /*your code here*/ }) 
    

    You can also put a "onload" attribute inside the body

    <body onload="run()">