Single firing click event listener [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
I am currently building a JavaScript tool that takes in an address and runs it through a series of functions that outputs a Boolean value of whether the address is within a range of addresses. From this output the tool will dynamically add cards to the page based on the output. My problem at the moment is that if the event listener is clicked more than once it just continues to add the cards to the page. Is there an event listener that will take in a click event but only fire the first time the event is triggered? If not am I better off just writing a function to remove the click event once it has been fired? I am more than happy to include the code from the project but seemed unnecessary.

addEventListener has an option to listen for an event only once:
const button = document.querySelector("button");
button.addEventListener("click", onClick, {
once: true
});
function onClick() {
window.alert("Clicked!");
}
<button>Try clicking me twice</button>
This option is supported in all major browsers except for Internet Explorer (which has been discontinued)

Related

How to listen for an event that is happening to another element using JavaScript only? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
So from my understanding of event listeners is that the action only applies to the element that you used the event listener on.
But say that I wanted to listen for a click on one element that is in a separate div, but the element I want to apply certain actions on is in a separate div. What would be the best way if possible in doing so?
To help visualize, im looking at this users example.
https://ryannathanwilson.github.io/Rock_Paper_Scissors/
And so from what it looks like it listens for a click on any of the buttons, and then the clicked button shows up in another area and then performs a specific action. Is this possible? Or am I understanding his code incorrectly and the elements that appear at the bottom when clicked is the original element?
You can write a function like this:
function myfunc(){
document.getElementById("abcd").value = "Lorem Ipsum";
}
And call this function on the element you want to listen on:
<button onclick=myfunc()>Click me! </button>

Which event is triggered first on form save button [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
When you click "save" on a form , 2 jquery events I know get triggered. Actually I was using both but just today I realised one is enough :) . But still let's say you define both, which one would be triggered first. $("#btnSaveForm").click() or $("#btnSaveForm").submit()
What I also noticed as soon as a click handler is triggered, the GUI basically freezes as long as this click handler is completely executed(Even if there is a ajax call done inside the handler). But still the GUI remembers what you did and executes the clicks afterwards.
The order of which things are executed is as follows :
User clicks the submit button
The onclick function is executed
The browser submits the page to the url specified in the action of
the form
As you can see in this example, the button triggers first and the form triggers right after.
Maybe you are using sync ajax handler if you are experiencing Frozen GUI after submitting your form.

What is the best way to trace event propagation in Chrome? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Suppose an application has a chat module containing messages. There is a jQuery click handler on said chat module that fires a series of focus related events whenever it detects a click.
Now suppose that when certain messages within the chat module are clicked, the event never bubbles up and a click event is never activated on the chat module.
How would I troubleshoot such an issue? I have a theory that somewhere in the application an intermediate element between the message click handler and the chat module click handler which is capturing the event and calling e.stopPropagation on it. There is currently no tools in Chrome to trace event propagation though.
What is the best way to trace event propagation in Chrome?
Override jQuery.Event.prototype.stopPropagation with a function containing a debugger statement:
jQuery.Event.prototype.stopPropagation = function() {
debugger;
};
From there perform the user actions that will fire the stopPropagation event; this time however a the debugger statement will cause the JavaScript to pause execution and you can observe the stack trace to see where it is being called from.
Put an ever-loving crap-ton of console logs everywhere. Not the most elegant solution, but it allows me to see what information is getting passed and where any and all hiccups are.
console.log(var);
I may suggest you to open the Chrome console (F12) go to elements, select the element of your interest and with right mouse click set the subtree modification.

What happens when onkeyup script is retriggered while running? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I was wondering, what happens when a button is pressed and the specified "onkeyup" script is triggered while that script is running?
Thanks for your time!
When you have a function, that is bound to an event, and this event triggers, then this function will run as many times, as the event triggers. So if you trigger keyup event 5 times, the corresponding function will run 5 times as well.
If you want to avoid it, you should do some checkout. For example, I often do it, when work on some sliders. Here's an example:
$('.selector').click( function() {
if ( ! $(this).is(':animated') ) {
// further actions will occur, only if the ".selector" is NOT animated,
// regardless how many times you click it.
}
});

What is the major difference between Event and EventHandler? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
The reason behind of posting this question is to understand the basic differences and use cases of the Event and EvenHandler.
Event:- In computing, an event is an action or occurrence detected by the program that may be handled by the program. Typically events are handled synchronously with the program flow, that is, the program has one or more dedicated places where events are handled.
EventHandler:-
A function or method containing program statements that are executed in response to an event. An event handler typically is a software routine that processes actions such as keystrokes and mouse movements. With Web sites, event handlers make Web content dynamic. JavaScript is a common method of scripting event handlers for Web content.
Very basic example is you click on login button on facebook, there is corresponding event handler for click on login button which tells facebook app that user trying to login
You cannot compare the both Handler and event.
All Handlers are stored with a HandlerManager which also manages dispatching any type of event (including new ones you create yourself).
More over
Handlers are passed an Event object with all the Event details as their only parameter i.e Event
They may mean different things, depending on a specific context. A general meaning of an event is a an action or occurrence somewhere in the program; normally events are handled by the program in modules called handlers.
For example in Java you may an event triggered by the push of a button; what the program does when that button is call the code in the handler for that particular event.

Categories