0

I have this button in HTML and I need to check if the button has been pressed to activate another function or not.

<button id="generator" class="button ripple" onclick="myFunction()">Berechnen</button>

    function printDiv(print) {
        if(document.getElementById('generator').clicked == true){
            var printContents = document.getElementById(print).innerHTML;
            var originalContents = document.body.innerHTML;
       
            document.body.innerHTML = printContents;   
            window.print();
            document.body.innerHTML = originalContents;
            
        }
        else{
            alert("pls select")
        }
       
   }
obind
  • 105
  • 1
  • 8

2 Answers2

0

You should use this function directly on button click:

    <div id="printDiv1">Printable Content</div>
<button id="generator" class="button ripple" onclick="printDiv('printDiv1')">Berechnen</button>
<script>
function printDiv(print) {
        //if(document.getElementById('generator').clicked == true){
            var printContents = document.getElementById(print).innerHTML;
            var originalContents = document.body.innerHTML;
       
            document.body.innerHTML = printContents;   
            window.print();
            document.body.innerHTML = originalContents;
            
        //}
       // else{
       //     alert("pls select")
        //}
       
   }
</script>

here the variable passed in the method printDiv(print) can be defined earlier or can be id of the div to print.

MSQ
  • 489
  • 5
  • 15
  • i have 2 buttons one to generate and the other one to print. but you can only print after you used one time the generate buttonhow can i make this – obind Feb 17 '21 at 08:18
  • the question was about checking one button click, so I answered for that to directly use onclick. method print. – MSQ Feb 17 '21 at 08:27
  • if there are 2 buttons, one for generate and 1 for print, you can use 2 functions sending same arguments(div ID) but 1 button will use generating function, the 2nd button will use printing function onclick. – MSQ Feb 17 '21 at 08:29
0

document.getElementById('button').onclick = function() {

   alert("button was Clicked");

};
<h3>Click on button below </h3>
<button id="button"> Click Me! </button>