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

266
Views
¿Cómo obtener un elemento HTML que llamó a una función?

Tengo elementos HTML con el atributo 'onclick' como este:

 <div class="item add pt-3" data-toggle="modal" data-target="#uploadModal" onclick="myFunction();"><i class="ni ni-fat-add"></i></div>

Más adelante en un script quiero tener this elemento para manipularlo.

 myFunction(){ $(theElement).data('target'); //example }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

 function myFunction(myEvent){ console.log( myEvent.getAttribute('data-toggle')) }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="item add pt-3" data-toggle="modal" data-target="#uploadModal" onclick="myFunction(this);"><i class="ni ni-fat-add"></i>click me</div>

about 4 years ago · Juan Pablo Isaza Report

0

Solo envíalo:

 onclick="myFunction(this);"

Y luego lo obtienes como el primer argumento de tu función:

 function myFunction(theElement) { $(theElement).data('target'); }

Pero debería considerar agregar el evento con JavaScript en primer lugar:

 <div class="item add pt-3" data-toggle="modal" data-target="#uploadModal">...</div>

Y su archivo JavaScript podría contener algo como:

 // add click listeners to all DOM elements that have the class "add" $('.add').on('click', function() { $(this).data('target'); });

O sin jQuery y JavaScript moderno:

 document.querySelectorAll('.add').forEach(el => { el.addEventListener('click', () => { el.dataset.target; }); });

Si el elemento se crea dinámicamente, podría considerar usar la delegación de eventos .

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!