Skip to content
Advertisement

data-id always returns the same value

I am calling an api using ajax which returns me a list of available coaches in a particular area. I am then displaying each coach and his details(firstname, picture, email etc) in a div using foreach loop. Each div has a contact button which should send an email to the coach so in order to get the coach email, I have put a hidden span element in each coach details and assigned it the value of coach email. Now, when I try to get the value of that span, I always get the value of first coach in the list. My HTML code for displaying the coaches:

JavaScript

Button which triggers the click event:

JavaScript

And this is how I am trying to get the coach email on button click:

JavaScript

Advertisement

Answer

If the button has a common parent with your hidden field you can use closest to get this common parent and find to go down to your hidden field. For example if your button and your hidden field are in the same div with the class “product_div’ :

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement