Javascript what was clicked




















You can find the target element in event. Use delegate and event. I know this post is really old but, to get the contents of an element in reference to its ID, this is what I would do:. Here's a solution that uses a jQuery selector so you can easily target tags of any class, ID, type etc. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more.

How to get the element clicked for the whole document? Ask Question. Asked 9 years, 9 months ago. Active 4 months ago. Viewed k times. How to get only the element I clicked on? Improve this question. Community Bot 1 1 1 silver badge. The handler function executes in the scope of the element it is added to, here the document.

You will need to get the target property of the event object. Add a comment. Active Oldest Votes. If you take a break between clicks, the count will reset. Bubbles Yes Cancelable Yes Interface MouseEvent Event handler property onclick If the button is pressed on one element and the pointer is moved outside the element before the button is released, the event is fired on the most specific ancestor element that contained both elements. Known workarounds for this bug: Set cursor : pointer; on the element or any of its ancestors.

Use a typically interactive element e. Stop using click event delegation. Introduction to events auxclick contextmenu dblclick mousedown mouseup pointerdown pointerup. An event listener takes over the handling of the event being listened for, similar to putting someone else in charge of calling the handler when an event occurs:. The code above adds an event listener to myButton , which stores a reference to the element that we wish to receive events on.

The parameters for addEventListener are as follows:. The second parameter is the function to invoke when the event occurs. In this case, the function greet will be invoked when the event takes place.

Note: Once again, the parentheses are omitted to prevent the function from being invoked immediately. Note: A function expression can be used here as well. In fact, it is very hard to find an HTML element that cannot have an onclick event assigned! If you choose to make an element clickable, help the user by applying a cursor: pointer style to that element — this provides a visual indicator that a given element is clickable:. When clicked, the greet function is invoked.

Learn more about setting style to HTML elements by following this link. There are two additional important notes to keep in mind when working with onclick events:. The example below explores the interaction of each of these three events. All three events are assigned to the same button, with each receiving a different set of code to run:.



0コメント

  • 1000 / 1000