I've got the following jQuery expression, Inside the function I need a reference to the object that was clicked, is there a way to do this?
$('#tagList li').click(function() {
/* contents */
});
Another way to get the element we clicked on in the event handler is to get it from the event object which is the first parameter in the event handler function. And the following JavaScript code: const onClick = (event) => { console. log(event.srcElement.id); } window.
So onclick creates an attribute within the binded HTML tag, using a string which is linked to a function. Whereas . click binds the function itself to the property element.
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.
The click() method simulates a mouse-click on an element. This method can be used to execute a click on an element as if the user manually clicked on it.
Use
$(this)
you can use the return value
$("#tagList li").bind("click", function(e) {
alert(e.currentTarget + ' was clicked!');
});
or if you want, you can simple point to the object in jQuery mode
$("#tagList li").bind("click", function(e) {
alert($(this) + ' was clicked!');
});
if you're new to jQuery, I strongly suggest you to see some screencasts from Remy Sharp in jQuery for Designers, they are great to understand a little bit of how jQuery works, and better yet, how to use the console.log() in order to see the objects that you can use!
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