0

I'm a newbie to javascript and web developement in general but what i'm trying to do is: create different forms and depending on which button is clicked a certain code should run( for now just display something).

Here is what i've done

<script>
    function init() {

      //this.onclick = showAlert;
      var p1 = document.getElementById('btn-hello');
      var p2 = document.getElementById ('btn-web');
      var p3 = document.getElementById('btn-prive');

      p1.onclick = showAlert('msghello.txt');
      p2.onclick = showAlert('msgweb.txt');
      p3.onclick = showAlert('msgprive.txt');

    function showAlert(file) {
      // Requiring fs module in which 
      // readFile function is defined.

      $.get(file, function(data) { alert(data)}, 'text');

  }
    </script>

enter image description here The other buttons are the same just the "id" changes.

The problem is that whatever button i click it runs the code for every button. I also tried using this argument but still does not work.

0 Answers0