Good day.

There is a generated page, on it - for example, 50 buttons:

<a data-toggle="n1" href="1" id="button" class="c1">Ответить 1</a> <a data-toggle="n2" href="2" id="button" class="c2">Ответить 2</a> и т.д. 

I know how to call an event when I click on it, but after clicking I have to open the form, let the user fill it in and send it to the server with an Ajax. But how can I pass the button ID in the event to distinguish them from each other? Perhaps there is a special tag, the value of which you can pick up?

When I click on id="button" the event works, and how do I get the value from the data-toggle href class or any other specials? tag, to understand that they clicked on the 2nd, and you need to work with the 2nd entry in the database.

Thank.

  • @barseon, you set different classes, and the ID of all elements is the same? )) You should have exactly the opposite. Within one page there can not be two or more elements with the same ID attributes. Take this phrase in a frame, hang it in a visible place and repeat every day like a mantra;) - Deonis
  • id name, for example, button, should appear on the page only once, 2 or more times the same id lead to errors, see the example @xfloooo how JS behaves when accessing the same id: jsfiddle.net/m4d97z8k/1 - MasterAlex
  • Yes, I'm sorry, I threw it hastily, ctrl + c ctrl + v. - barseon

1 answer 1

Use this - http://jsfiddle.net/m4d97z8k/

HTML

 <a data-toggle="n1" href="1" id="button" class="c1">Ответить 1</a> <a data-toggle="n2" href="2" id="button" class="c2">Ответить 2</a> 

Javascript

 $('a').click(function(){ alert($(this).attr('data-toggle')) return false })