Onclick which button clicked




















Learn more about JavaScript functions by following this link. In addition to defining the handler on an individual HTML element, you can also dynamically add a handler using JavaScript code.

There are two ways to accomplish this:. In both cases, the Button in the HTML document itself will not initially have an onclick event assigned to it when defined:.

In the HTML segment above, the button has no events assigned to it. It is a plain button with text. Both approaches to adding the event handler with JavaScript require us to grab a reference to the button and append an event to it. Below is an example of obtaining a reference to our button from the DOM:.

There are various ways to get a reference to a DOM element. The code then prints the reference to the console, which is simply the HTML for defining the button:. Now that we have a reference to the element, we can use the reference to assign an event handler. These are called parameters in JavaScript. So, let's select our freeCodeCamp text and write the function to change its color to blue, green, and orange-red:.

The block of code in the function takes the name variable where we stored our freeCodeCamp text , then set the color to whatever we passed into the changeColor functions in the HTML buttons.

In JavaScript, there are multiple ways of doing the same thing. You can also do this with onclick, but lets take another approach here. This time around in our script, we need to select the button too not just the freeCodeCamp text.

We can also separate our function totally from the eventListener and our functionality will still remain the same:. One of the best ways to learn is by making projects, so let's take what we've learned about the onclick and "click" eventListner to do build something. When you visit a blog, you often see excerpts of articles first. Then you can click on a "read more" button to show the rest. Let's try to do that. And there's a button we already attach an onclick to. The function we want to execute is showMore , which we will write soon.

Without a CSS, this is what we have:. It's not super ugly, but we can make it look better and act the way we want it to. So we have some CSS which I will explain below:. The paragraph tags inside of it have a font-size of 18px, and then we gave them a maximum height of px. Due to the max height of the article element, all the text won't be contained and will overflow.

To fix this, we set overflow to hidden in order not to show that text at first. The transition property ensures that every change happens after 1 second. If and how can this code be used?

I know this is an old thread but on my modern browser FireFox 78 your implementation of this did not work as intended. My function is triggered by an onkeyup event of an input text box, but this was using the window node.

Instead, I had to use event. Prateek Prateek 7 7 silver badges 6 6 bronze badges. I like this idea for dynamic functionality. Prateek Firefox, latest. I only needed the source, so I worked around it now by passing this as parameter in onClick actually, not using onClick but onChange, is that maybe the problem? I've also checked around a bit and there seems to be a lot of confusion about this event variable going around - is it an "implicit" parameter or does it have to be stated explicitly as argument i.

Is it only available when assigning the event listener via addEventListener? I've played around, but couldn't get it to work. FYI: The global event object is available in chrome, not firefox. As of , event. I think the id attribute needs to start with a letter. Could be wrong. You could go for event delegation This won't work.

So, no event argument will be passed to the function. And the value of the "id" attribute can be any string. It doesn't have to start with a letter. Jason Actually, in all the good modern browsers, the e argument is generated automatically. If it isn't, then we must be dealing with IE, which instead provides that useful object via window. Thanks this worked for me even when my id were dynamic and no other answer seemed to work.

Thanks a lot. Show 4 more comments. Mohanraj Mohanraj 3 3 silver badges 2 2 bronze badges. In other words, detail will be 2 for a double-click, 3 for triple-click, and so forth.

This counter resets after a short interval without any clicks occurring; the specifics of how long that interval is may vary from browser to browser and across platforms. The interval is also likely to be affected by user preferences; for example, accessibility options may extend this interval to make it easier to perform multiple clicks with adaptive interfaces. Any click events will be fired at the underlying element s instead.



0コメント

  • 1000 / 1000