Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

107
Views
obteniendo el parámetro EVENT con eventListener mientras usa el método Bind (clase)

Estoy usando addBtn.on('click', list.addItem.bind(list)); . ¿Cómo puedo obtener el parámetro de event para poder implementar event.preventDefault() dentro addItem(){} . ¿Hay una manera de hacer eso?

 const addBtn = $('#addBtn'), itemListText = $('#itemListText'), textInput = $('#addToListInput'); class Lists { constructor() { this.currentList = []; } currentListCount() { return this.currentList.length; } addItem() { if (textInput.val().length > 0) this.currentList.push({ item: textInput.val(), checked: 'false', }); itemListText.text(`Item List (${this.currentListCount()})`); textInput.val(''); } } const list = new Lists(); addBtn.on('click', list.addItem.bind(list));
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Lo haces exactamente de la forma en que lo hubieras hecho sin usar bind : aceptando el parámetro:

 addItem(event) { // ...use `event.preventDefault()` etc. here... }

La función bind return llama a la función original con todos los argumentos que recibe, por lo que pasa el evento a tu método.

Ejemplo en vivo:

 const addBtn = $('#addBtn'), itemListText = $('#itemListText'), textInput = $('#addToListInput'); class Lists { constructor() { this.currentList = []; } currentListCount() { return this.currentList.length; } addItem(event) { event.stopPropagation(); if (textInput.val().length > 0) this.currentList.push({ item: textInput.val(), checked: 'false', }); itemListText.text(`Item List (${this.currentListCount()})`); textInput.val(''); } } const list = new Lists(); addBtn.on('click', list.addItem.bind(list)); $("#wrapper").on("click", () => { console.log("Wrapper saw click event"); });
 All of the elements below are in a wrapper that logs when it sees a click. Notice that it sees clicks every except when you click the Add button, because the Add button uses <code>stopPropagation</code> on the event object it receives. <div id="wrapper"> <div id="itemListText">Item List (0)</div> <input type="text" id="addToListInput"> <input type="button" id="addBtn" value="Add"> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!