This post was originally published on this site

If you’ve ever used a form on a website, chances are you’ve interacted with JavaScript. It’s the technology that makes things like buttons and drop-downs work.  But what is it, and how does it work?

Person on computer learning how to use javascript onclick event

Download Now: An Introduction to Java & JavaScript

In this post, we’re going to take a look at the onClick event in JavaScript. We’ll start by explaining what it is and then show you how to use it yourself. After reading this post, you’ll be able to add interactivity to your websites using JavaScript.

What is JavaScript onClick event?

JavaScript is a programming language that can be used to add interactivity to websites. The onClick event is one of the most commonly used events in JavaScript. It occurs when an element is clicked on.

For example, say you have a button on your website. When a user clicks that button, you can use the onClick event to do something. Maybe you want to display a message or redirect the user to another page. Whatever the case may be, the onClick event makes it possible.

To use the onClick event, you must add code to your website. Don’t worry if you’ve never coded before – we’ll walk you through it step by step.  

How to use an onClick event?

First, you must select the element to which you want to add the onClick event. For our button example, we would use the

Next, add an attribute to your element. This attribute is called onClick. This attribute’s value is the code you want to run when the element is clicked.

Here’s what our code would look like with the onClick event added:


When a user clicks on this button, they will see an alert popup that says, “Hello, world!” You can change the message to anything you want.

You’re not limited to just displaying a message, either. You can also use the onClick event to run other javascript code. For example, you could redirect the user to another page using the following code:


This button would redirect the user to when clicked.

As you can see, the onClick event is a powerful way to add interactivity to your website. There are endless possibilities for what you can do with it!  

JavaScript onClick Event Example

Now that we’ve gone over the basics of the onClick event, let’s look at an example.

In this example, we’re going to create a button that, when clicked, will change the color of the text on the page. To do this, we’ll need to use two things:

  1. The onClick event

  2. The javascript function document.getElementById()

The onClick event will be used to run a function when the button is clicked. The document.getElementById() function will be used to get an element on the page so that we can change its style.

Here’s what our code will look like:

Let’s break this down piece by piece.

First, we have our button:


This button has the onClick event added to it. The value of the onClick event is the changeColor() function. This means that when the button is clicked, the changeColor() function will run when the button is clicked.

Next, we have our javascript code:  


This is the code that will run when our button is clicked. First, we create a function called changeColor(). This function doesn’t take any arguments.

Inside our changeColor() function, we use the document.getElementById() function to get an element on the page. In this case, we’re getting the element with the id of “myText”.

Once we have our element, we can change its style. In this example, we’re changing the color property to red. This will make the text red.

Finally, we have our element:


This is some text.

This is a simple paragraph element. The only thing different about this element is that it has an id attribute. The document will use this id attribute.getElementById() function to find the element on the page.

And that’s it! When you run this code, you should see a button and some text on the page. When you click the button, the text should turn red.  

Final Tips For Using JavaScript onClick Events

Here are some final tips for using onClick events:

  • Make sure your code is valid. Invalid code can cause problems with your website. Use a tool like JSLint to check your code for errors.

  • Test your code in different browsers. Different browsers can interpret code differently. Ensure your code works the way you want it to in all major browsers.

  • Be careful not to overuse the onClick event. If you use it too much, your website can become overwhelming and confusing for your users. Use it sparingly and only when it makes sense to do so.

That’s all there is to know about the onClick event. You can now start using it on your own website.