Javascript Onclick Not Working

Javascript Onclick Not Working
Content List

Introduction JavaScript is a popular way to write code. It's used by more than 95% of the websites we visit every day. When you click a button, you might see a whole page change, a form field open, or a pop-up box appear. From a programmer or developer's point of view, how can we add this functionality and handle how users interact with the website? JavaScript has built-in functions that let you control what happens on a site when it comes to interaction. javascript onclick.

In JavaScript, there are two kinds of events:

Event Listener: Listens for the event and waits for it to happen.

Event Handler: This code is run when an event happens.

This article will teach you about the onClick event, which is the most used event handler in JavaScript. There are other event handlers for things like hovering over an element or pressing a key on the keyboard, but we will focus on the onClick event in this article.

The onClick event is used to do certain things when a button is clicked or when an HTML element is touched.

Now, we'll give you an example to show you how the onClick event works.


Javascript Onclick Not Working


Example: Using onClick to change text javascript onclick

In this example, we will use the onClick event to change a piece of text when a button is clicked. First, we'll make a tag for a paragraph and give it the ID "paragraph" so we can use it later. We'll make a button with an onClick event and call the "change" function when the button is clicked.

<p id="paragraph">Linuxhint</p> <button onclick="change()">Change!</button>
We will make a flag variable in the script file so that we can check the status of the text in our HTML paragraph tag. Then, we'll write a function that tells the computer what the "change" function is. In the function definition, we'll make a "if" statement in which the flag variable will be used to check the status. We'll also change the words and make changes to the flag. It's not too hard to figure out!

var a = 1; function change(){ if (a==1) { document.getElementById("paragraph").innerHTML = "Linuxhint is awesome" a = 0; } else { document.getElementById("paragraph").innerHTML = "Linuxhint" a = 1; } }
Good enough! After writing all this code, we'll run it, go to our browser, and click the button we just made. The text should change from "Linuxhint" to "Linuxhint is awesome" when you click the button.

We can use the same method to change the content of our website no matter where we are. We can use it to change an image or do just about anything else we can think of.

HTML5 and JavaScript traps: When your onClick buttons don't fire after your onChange inputs do.

I started to answer Mike Doyle's question, but then I realized it was getting too long. So instead, I decided to write this little blog.

This is not a new problem, as Mike said (see my code below or read Mike's question). I've run into this problem a lot in UI5 projects, and the workarounds are just workarounds that I'd rather not use, such as:

if it's possible, do it the way Mike does.

using the setTimeout function (if possible, may need additional boilerplate code)

if you can switch to live change (may need additional boilerplate code)

If you can, put all the code from the event handlers into the press of a button (may need additional boilerplate code)

Using only code in the change handle that doesn't stop the press event from happening (like not using alert() or certain other operations that change the DOM)...

I can't give a general rule because I've taken different steps depending on the situation.

See the following native HTML5/JS examples, which I've tested on Safari 12.1.2, Chrome 76, Firefox 68.0.2, and IE 11 (or this jsbin for live demos):

1. It works as it should: two events always happen in the right order.

<input onchange="console.log('onchange')">
<button type="button" onclick="console.log('onclick');">Click me</button>

In general, we want to see "change" before "onclick" in the console output when we change the value of an input field and click a button. This works great.

2. Does what it's supposed to do: two events always happen in the right order

<input onchange="console.log('onchange');this.nextElementSibling.style.color='green';">
<div>Make me green in 'onChange'</div>
<button type="button" onclick="console.log('onclick');">Click me</button>

Even this works perfectly well in every browser I've tried. First, the console shows "change." Then, CSS changes the color of the text to green. Finally, the console shows "click." So this small change to the DOM (the change to CSS) is not causing any problems.

3. Pay attention: only the very first click fires change (all browsers)

<input onchange="console.log('onchange');this.nextElementSibling.style.display='none';">
<div>Hide me in 'onChange'</div>
<button type="button" onclick="console.log('onclick');">Click me</button>

So let's make another change to the DOM, but this time let's make a "bigger" change that hides a DOM element. In this case, it's interesting that only "change" will show up in your console the first time, and this will be the same across all browsers. When you change the value of the input field again and then press the button again, everything works as it should (because the div> is already hidden).

4. Pay attention: in Chrome, IE11, and Safari, the only change is fired.

<input onchange="console.log('onchange');alert('onchange')">
<div>Show alert() in 'onChange'</div>
<button type="button" onclick="console.log('onclick');">Click me</button>

When using an alert() in onchange it gets weird. Firefox shows "onclick" in the console after the user closes the alert(), but the other browsers don't show "onclick" for some reason (meaning onclick is not triggered). The reason may be that change is triggered when the focus leaves the input field. When the dialogue is opened and the user clicks "ok," there can't be a click on the button because the button doesn't even have the focus. I think this is why Chrome, IE11, and Safari act the way they do. But I kind of like how Firefox works here because it seems more natural to me.

Alerts are thought to be bad these days, by the way.

5. Pay attention: onchange is called AFTER onmousedown (all browsers)

<input onchange="console.log('onchange');alert('onchange')">
<div>Show alert() in 'onChange'</div>
<button type="button" onmousedown="console.log('onmousedown');">Mouse Down</button>

Then we could probably use ommousedown here. We could, and it does work in all of the browsers I've tried. But the change event of the input field happens after the button's mouse down event. So, the order of things has changed a little. I need to pay attention to this.

6. Pay attention: the only change is called, and onmouseup is never called in Chrome and Safari.

<input onchange="console.log('onchange');alert('onchange')">
<div>Show alert() in 'onChange'</div>
<button type="button" onmouseup="console.log('onmouseup');">Mouse Up</button>

So, let's give onmouseup a try. In Firefox and IE11, onmouseup works the way it should and in the right order. But neither Chrome nor Safari does anything when the mouse is up. I think this is odd.

You could also try out input, but I'll leave that as your task...

7. In the end

Well, as a JavaScript/WebDeveloper in general, this doesn't make things easier:

We have different browsers that act in different ways. This isn't a bug in UI5, but in how the browser works.

This problem isn't even new; it's been going on for a long time. I don't know why browser makers aren't trying to fix it.

All you can do is

testing your code on different browsers

Make people aware by telling other UI5 developers that if they can, they should try to avoid putting code in onchange that changes the DOM (I can't always do this myself). In that case, best of luck.

find a way around it based on your situation.

I know that this isn't a good ending. If anyone else comes to a better conclusion, I'd be glad to hear it. This blog is more about getting people to pay attention.

Conclusion

The onClick event is explained in this article. In this article, you saw how the onClick function works in the real world. The onClick event is so easy to use that even a beginner can start making things happen with it. You can keep learning JavaScript, working with it, and getting more experience at linuxhint.com to get a better handle on it. I appreciate it.