0

I'm a django developer not good at javascript. I have a django template i really want to use Javascript for. i tried reading online for help but i'm not understanding

I want to add an addEventlistener to this code below

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

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

I know JS is really good at this.

sylucck
  • 23
  • 6
  • Does this answer your question? [addEventListener vs onclick](https://stackoverflow.com/questions/6348494/addeventlistener-vs-onclick) – blurfus Jan 26 '22 at 23:20

3 Answers3

2

So presumably you want to add an event listener to the button so that it submits when clicked. You can go about that by adding an event listener to the element between two script tags <script></script> or in your .js file. The code would look like:

document.getElementById("send-payment").addEventListener("click", function() {
     /*
          Action Here...
     */
     alert("Request Sent!") // This will throw up an alert to the user.
})

From what I gather this should be a simple solution, you can find more documentation here: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener

cmcnphp
  • 400
  • 1
  • 4
  • 14
1

Make this

const button = document.getElementById("send-payment");

button.addEventListener("click", function() {
  document.getElementById("request-info").style.display = "block";
})
#request-info {
  display: none;
  color: green;
  font-size: 12px;
  margin-bottom: 10px;
}
<div id="request-info">Request sent</div>
<button id="send-payment">Send request</button>
Co-7
  • 170
  • 11
1

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>
<script>
 document.getElementById("send-payment").addEventListener("click", function(){
 document.getElementById("response").innerHTML = "Request sent";
})
</script>

Here is the link on w3schools: https://www.w3schools.com/jsref/met_document_addeventlistener.asp

Gabrielle
  • 36
  • 3