Home > Back-end >  I want help in adding an event listener
I want help in adding an event listener

Time:01-27

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  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.

CodePudding user response:

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>

CodePudding user response:

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

CodePudding user response:

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

<div  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

  •  Tags:  
  • Related