Search code examples
javascripthtmldjangoformsaddeventlistener

I want help in adding an event listener


I have a Django template I really want to use JavaScript for. I tried reading online for help, but I'm still not understanding it.

I want to add an addEventListener() method to this code below:

<div
  class="box-element hidden"
  id="request-info"
>
  <button id="send-payment">Send request</button>
</div>

Also, I want it to display a response when submitted; response like "Request sent". I know about console.log(), but I don't want it in console.log().


Solution

  • You need a space to display the message "Request sent". Here I use the <p> tag:

    <div
      class="box-element hidden"
      id="request-info"
    >
      <button id="send-payment">Send request</button>
      <p id="response"></p>
    </div>
    <script>
      document.getElementById("send-payment").addEventListener("click", function () {
        document.getElementById("response").innerHTML = "Request sent";
      });
    </script>
    

    Here is the link on W3Schools.