Issues with onfocus and onclick events not functioning as expected in HTML

I have encountered what appears to be a common yet perplexing issue. You can view a draft of the page here. The specific behavior I am aiming for is as follows:

To execute a calculation, use

<script type="text/javascript" src="amp.js"></script>
and call it with <body onload="amp()">. Due to the lengthy nature of amp.js, I will not include it here. Assume all variables are correctly declared and initialized, and my scripts are linted. Then, reset the progress bar and counter to zero:

<progress id="progress" value="100" max="100"></progress><output class="percent" id="percent">100</output><span class="prozent">%</span>

...when the user focuses on changing input parameters for a new calculation:

<input type="number" name="theta0" id="theta0" value="0.1" min="0.0" max="1.6" step="0.1" onfocus="reset()">

...using

<script type="text/javascript" src="../reset.js"></script>
:

/*jslint browser: true, devel: true */
function reset() {
  "use strict";
  document.getElementById('progress').value = 0;
  document.getElementById('percent').innerHTML = 0;
}

Once ready, the user can initiate a new calculation with

<input type="button" value="Evaluate" onclick="go = setInterval(animate, 10); amp()">
which will animate the progress bar and display the results. Details of
<script type="text/javascript" src="../animate.js"></script>
are as follows:

/*jslint browser: true, devel: true */
var go = 0;
var value = 0;
var max = 100;
function animate() {
  "use strict";
  if (value >= max) {
    clearInterval(go);
    return;
  }
  var pBar,
    percent;
  pBar = document.getElementById('progress');
  percent = document.getElementById('percent');
  value += 1;
  pBar.value = value;
  percent.innerHTML = value;
  if (value === max) {
    clearInterval(go);
  }
}

The onfocus and onclick events are not functioning properly. However, individually calling reset or animate with <body onload=""> does work. The console provides no clues. For helpful suggestions, refer to this list here. Although it may seem unrelated, it has been noted that faulty CSS can lead to unusual issues. Here's mine:

input {
  background-color: snow;
  border: 1px solid darkseagreen;
  box-sizing: border-box;
  color: indigo;
  font: 1em "Computer Modern", serif;
  width: 10%;
}
input[type=number] {
  border-left-style: none;
  border-right-style: none;
  padding-left: 5px;
  vertical-align: middle;
}
input[type=number]:focus {
  background-color: white;
  border: 1px solid black;
  border-left-style: none;
  border-right-style: none;
  color: black;
  outline: none;
}
input[disabled] { background-color: lightgray }

...and...

input[type=button] {
  background-color: ghostwhite;
  background-image: linear-gradient(ghostwhite, #E0FFFF);
  border: 3px solid #4CC417;
  border-radius: 7px;
  color: indigo;
  font: 1.1em "Trebuchet MS", sans-serif;
  margin-left: 10px;
  padding: 5px;
}
input[type=button]:hover { border: 3px solid orange }
input[type=button]:focus {
  background-color: aliceblue;
  background-image: linear-gradient(#D6F5F5, ghostwhite);
  border: 3px solid fuchsia;
  color: black;
  outline: none;
}

Any insights or recommendations would be greatly appreciated.

Update

Interestingly, the version found here somewhat functions. However, the issue lies in the improper functioning of the reset function, especially during subsequent calculations by the user. Additionally, an onchange event needs to be added to those input boxes.

Answer №1

So there's a problem:

/*jslint browser: true, devel: true */
var go = 0;
var value = 0;
var max = 100;
function animate() {
  "use strict";
  if (value >= max) {
    clearInterval(go);
    return;
  }
  var pBar,
    percent;
  pBar = document.getElementById('progress');
  percent = document.getElementById('percent');
  value += 1;
  pBar.value = value;
  percent.innerHTML = value;
  if (value === max) {
    clearInterval(go);
  }
}

The issue is with the usage/declaration of pBar and percent, causing the script to break. I don't know why this happens. After making some modifications, here's the revised script:

function animate() {
  if (value >= max) {
    clearInterval(go);
    return;
  }
  value += 1;
  document.getElementById('progress').value = value;
  document.getElementById('percent').innerHTML = value;
  if (value === max) {
    clearInterval(go);
  }
}

After these changes, the script works flawlessly. For further details, check out this link.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The collapsed Bootstrap navbar is failing to extend to reveal the menu links

I recently launched a Django website using a Bootstrap template. However, I encountered an issue where the expand menu feature of the navbar does not work properly on mobile devices. Can anyone provide guidance on how to resolve this issue? .nosh_color ...

Tips for consuming a REST API to retrieve JSON data and assign it to a variable for use in React JS

I'm currently working on developing a shopping application using React.js. I found inspiration from a sample application on https://codepen.io/paulkim/pen/oZLavq , and now I am looking to fetch product information from an API call. I have attempted to ...

Node.js Implementation of HTML Content

Currently, I am attempting to iterate through a list of items and generate HTML code to be passed into the view file: const itemWrap = '<div id="items"></div>'; userDetails.notes.forEach(item => { const itemE ...

Learning how to utilize getDerivedStateFromProps over componentWillReceiveProps in React

To address a deprecated error, I am looking to update my code from using componentWillReceiveProps to getDerivedStateFromProps. The component is being passed a date prop, and whenever this date changes, the getList function needs to be run with the new dat ...

Guide to incorporating animated material icons in vuetify

in Vuetify makes it easy to utilize mainstream material design icons through the use of the v-icon component. An example of this would be: <v-icon>home</v-icon> I am curious if animated material icons are supported and can be integrated in ...

Identify alterations in an input field after selecting a value from a dropdown menu

Is there a way to detect changes in the input field when selecting a value from a drop-down menu, similar to the setup shown in the image below? html: <input type="text" class="AgeChangeInput" id="range"/> js:(not working) <script> $(docume ...

Tips for solving the problem of page navigation using jquery-mobile

I have a jquery-mobile application with two actual HTML pages, but many more data-role="page" divs. The issue arises when transitioning from the first actual HTML page to the next one as the navigation stops working. I am not using any custom JavaScript, j ...

Trouble with activating dropdown toggle feature in Bootstrap 5

I recently upgraded to Bootstrap 5 and now my code is not functioning properly. <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria- controls="navbarCollaps ...

Retrieving a file URL from Sanity within a blocks array

I've been working on a website with Next JS and using Sanity as the CMS. While Sanity has schemas for images, I ran into an issue when trying to handle videos. The documentation recommends using GROQ queries to convert file URLs at the request level, ...

Preserving older items while removing a list item in React

I am facing an issue where I want to remove a specific item from a list. Whenever I click on delete, the correct item gets removed from the list content, but visually on the UI, it always shows the wrong list item being deleted. It seems like there may be ...

What is the best way to extract the ID of an element that triggers an OnChange event?

Just a heads up: The solution to my problem ended up being CSS code that was triggered by clicking on a widget. Once I realized it was CSS, along with the widget name and hover action, I was able to handle it successfully. Previous question before the PS ...

Utilizing bootstrap tools and experiencing a deficiency in organization

Looking to utilize utility classes, I am currently importing the latest version of Bootstrap (v4.0.0-alpha.6). However, upon searching through the node_modules files, I am unable to locate any "order-" utility rules. Has this feature been removed? The doc ...

How to use AngularJS directives to replace text with stars (*) in HTML

I am in need of a textarea control that has the ability to be masked, meaning that the text displayed should appear as stars instead of the actual characters. Since I might have multiple textareas in my form, saving the actual text in one variable and usi ...

Is it feasible to activate an action on a click of a Vimeo video?

Is there a way to activate an event by clicking if a video is set with background=1 and has no controls? This particular video is from Vimeo, using a plus account which allows for setting background=1. The scenario involves a Vimeo video playing on loop ...

Modifying a dynamic image path in real-time

I am faced with a situation where I have a webpage including an HTML pattern from a different folder. This included HTML contains a relative image path that needs to be altered so that the image can be loaded from both the included HTML and the source HTML ...

Issue encountered while trying to add information into MySQL

I am facing an issue with inserting data for two clients into a single table in MySql. The problem arises when trying to insert the details of the second client, and on subsequent attempts, neither set of data can be successfully added. MySql table: https: ...

Using JavaScript, create a set of buttons within a div element and implement

$(document).ready(function() { $('#b1').click(function() { $('#uch').toggle("slow"); }); $('#b2').click(function() { $('#uch2').toggle("slow"); }) }) Although I'm not a program ...

Struggling with a non-functional jQuery navigation tab onclick feature

Within my Django template, I am working with a series of nav-tabs that are being populated by a context variable: <div class="content-object"> <ul class="nav nav-tabs nav-fill" id="object_tab" role="tablist"> {% for object in object_ ...

Unraveling deeply nested array objects in JSON with Java Script/jQuery

I need help working with a JSON file that looks like the following: {[ {"name":"avc"}, {"name":"Anna"}, {"name":"Peter"}, {"Folder":[ {"name":"John"}, {"name":"Anna"}, {"Folder":[ {"name":"gg"}, ...

Is it just me, or is there a strange glitch with emojis: sharing the same ID and name, yet having different skin

Working with emoji is usually smooth sailing for me, but I've hit a roadblock with a stubborn bug. My script is designed to detect emoji and replace them, but there's a strange glitch with a few specific ones. Sometimes, the emoji appears differe ...