Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have two JavaScript onclick events in one element?

Tags:

javascript

Can we put two JavaScript onclick events in one input type button tag? To call two different functions?

like image 752
Piyush Avatar asked May 21 '10 10:05

Piyush


People also ask

Can you have two click events on same element?

The addEventListener() methodYou can add many event handlers of the same type to one element, i.e two "click" events.

Can you have two onclick events JavaScript?

So the answer is - yes you can :) However, I'd recommend to use unobtrusive JavaScript.. mixing js with HTML is just nasty.

Can a button have 2 onclick events react?

The first solution to perform multiple onClick events in React is to include all of your actions inside of a function and then call that single function from the onClick event handler. Let's explore how to do that in a React Component: import React from 'react'; function App() { function greeting() { console.

How do you pass two values on Onclick?

If you want to call a function when the onclick event happens, you'll just want the function name plus the parameters. Then if your parameters are a variable (which they look like they are), then you won't want quotes around them.


1 Answers

This one works:

<input type="button" value="test" onclick="alert('hey'); alert('ho');" />

And this one too:

function Hey() {     alert('hey'); }  function Ho() {     alert('ho'); } 

.

<input type="button" value="test" onclick="Hey(); Ho();" /> 

So the answer is - yes you can :) However, I'd recommend to use unobtrusive JavaScript.. mixing js with HTML is just nasty.

like image 70
rochal Avatar answered Sep 24 '22 11:09

rochal