I have numerous buttons on my page with the same class names. However these buttons have different ID's. How do i do this:
$(".vote").click(function(){ var id = $(this).{ID OF CLICKED BUTTON}; });
How can i make this pseudo code work?
Thanks
To get the clicked element, use target property on the event object. Use the id property on the event. target object to get an ID of the clicked element.
Chrome: Right-click anywhere on your screen, and click on the Inspect button or press CTRL + SHIFT + I or F12 from your keyboard. Firefox: Right-click anywhere on your screen, and click on the Inspect Element button.
The Html <button onclick=" "> is an event attribute, which executes a script when the button is clicked. This attribute is supported by all browsers. It is also used to call a function when the button is clicked.
To check if an element was clicked, add a click event listener to the element, e.g. button. addEventListener('click', function handleClick() {}) . The click event is dispatched every time the element is clicked.
$(".vote").click(function(){ var id = this.id; });
The ID is accessible directly from the element. There's absolutely no need to use a jQuery method.
With jQuery object (not necessary)
$(".vote").click(function(){ var id = $(this).attr('id'); });
Without jQuery object (faster)
$(".vote").click(function(){ var id = this.id; });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With