DEV Community

Cover image for Can an html button have two onclick function?
Lalit Kumar
Lalit Kumar

Posted on

Can an html button have two onclick function?

The question we will discuss in this article is “Can an html button have two onclick functions?”
Answer to the above question is YES!!! We can have two onclick functions in a single HTML button. Onclick just calls or invokes the functions we add to it. So, we can use two functions in a single HTML button.

How?

It is possible to use two functions in a single button because in the button tag we are invoking functions by clicking that particular button. Understand the working by assuming the onclick property as a function. As we know we can add one or more functions in a function. In the same way we can use one or more than one functions inside onclick property.
Although it is not recommended to use more than one function inside a button because it will look nasty. We should keep our code as clean as possible. This will help us and other programmers to work on that later.

I have answered the question above, this is it
But there is an alternative way, if you want to read that, check this link

Top comments (6)

Collapse
 
uzair004 profile image
Muhammad Uzair

JavaScript event listener should be used.

Collapse
 
whataluckyguy profile image
Lalit Kumar

Yeah totally agreed, but I thought i should it will help beginners

Collapse
 
uzair004 profile image
Muhammad Uzair

By using event handler with html onclick property yoy are still using JavaScript functions because that handler is still JS function. In future you may need to add more handlers or delete or change one which is really handy when using separate JavaScript event listeners. Also removing certain handler or all of them is easy using functions like removeEventListener. also reusability is awesome in separate file.

Thread Thread
 
whataluckyguy profile image
Lalit Kumar • Edited

but @wick3drose is also right, for a very small project that didn't need any other handlers, Beginner can use that

Collapse
 
whataluckyguy profile image
Lalit Kumar

Yeah totally agree with you, that is an great and that is how developers are doing things.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.