-6

I want to have multiple elements calling one function with onClick, is there anyway to get what element called it though?

I thought about giving each on an id and doing function(1) and then doing it based of that but is there anyway to dynamically do this?

jndne
  • 29
  • 7
  • What have you tried? – nicael Jul 28 '16 at 17:02
  • pls show us what you have tried. it helps us see what you did, and prove you did some research. – Iceman Jul 28 '16 at 17:06
  • @jndne, it appears that you've got a trend of downvoted questions due to not putting forth enough effort in asking and showing [what you've tried](http://whathaveyoutried.com). If you don't change your approach, you're going to find yourself question banned. – zzzzBov Jul 28 '16 at 17:06

1 Answers1

0

You have two ways to do it :

The easy way

Use jQuery, it's a JS library which will be really usefull for everything, and it's really easy to learn because it is JS.

You'll be able to do $(".myClass").click(function(){ // Code... });to select all elements with the myClass class (It uses CSS selectors).

The pure JS

Get the elements by tag, then you'll get an array and for each element in the array test if they have a certain class (give a special class to the elements you want to select). If they have the class, apply the addEventListener.

:)

Community
  • 1
  • 1
clota974
  • 495
  • 5
  • 16