Unexpectedly, Internet Explorer 11 is causing the "input" event to fire prematurely

While troubleshooting a JavaScript issue, I came across what seems to be a bug in Internet Explorer 11. I am reaching out here on StackOverflow for validation and to see if anyone else using IE11 can replicate this problem.

The problem arises when the value of a hidden input contains a HTML entity, triggering an input event as soon as the page loads.

To see this issue in action, check out this demo:

Here is the source code of the page:

<!doctype html>
<html>
<head>
  <script>
    document.addEventListener('DOMContentLoaded', function(){
      document.getElementById('foo').addEventListener('input', function(){
        alert('Crap!');
      });
    });
  </script>
</head>
<body>
  When you refresh the page in IE11, you will encounter an alert, whereas other browsers won't trigger any alert.

  <form>
    <input id="foo" type="hidden" name="utf8" value="&#x2713;" />
  </form>
</body>
</html>

I need help confirming if this is indeed a browser bug or if there is something wrong with my implementation.

Answer №1

If you come across this situation, consider using this specific "class" instead of relying solely on the onInput event.

const handleFieldChange = (function(inputElement, onChangeCallback){
        let intervalId = null;
        let previousValue = null;
        if(!(inputElement instanceof Element)) return new Error('inputElement is not a valid HTML Element');
        if(typeof onChangeCallback !== 'function') return new Error('onChangeCallback must be a function');
        return {
            watchFieldChanges : function(){
                if(intervalId){
                    clearInterval(intervalId);
                    intervalId = null;
                }
                else{
                    intervalId = setInterval(function(){
                        const currentValue = inputElement.value;
                        if(previousValue !== currentValue){
                            onChangeCallback(currentValue);
                            previousValue = currentValue;
                        }    
                    }, 100);   
                }    
            }
        };    
    });

To implement it, follow these steps:

const fieldWatcher = handleFieldChange(<**insert actual element reference here**>, function(newData){
console.log('New value detected: ' + newData);
});

Don't forget to use fieldWatcher.watchFieldChanges() within the onfocus and onblur events associated with the input field.

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

Alter the default time zone in JavaScript

PHP features a function known as date_default_timezone_set which impacts the GMT time that is used by the Date() command. Is there a way for this function to also impact JavaScript? Here is a custom function I have: function calcTime(offset) { d = n ...

Enclose each instance of "Rs." with <span class="someClass">

I'm facing an issue with the currency symbol "Rs." appearing in multiple places on my website. I want to enclose every instance of this text within <span class="WebRupee">. However, if it's already wrapped in <span class="WebRupee">, ...

What is the best way to retrieve both a response JSON and headers using Got?

Currently, I am utilizing Got for sending requests to a Strapi API from Node.js. The code snippet demonstrates how I achieve this: res.setHeader('Content-Type', 'application/json') try { const request = req.query.request const decod ...

The issue with the transition element not functioning properly in the hamburger menu is being encountered while using Tailwind CSS alongside

I am currently working on creating a responsive navigation bar in nuxt3/vue. <template> <nav class="flex justify-between items-center w-[92%] mx-auto gap-4 border border-2 border-black p-2" > <div> <span cla ...

Is it possible to retrieve 2 arguments within a function in a non-sequential manner?

Let's say there is a function with arguments A, B, C, D, and E. Function(A, B, C, D, E) However, not all arguments are needed all the time. For instance, only A and C are needed in some cases. Currently, I would have to call the function like this: Fu ...

Provide Arguments to a Function in Express JS

How's everything going? I'm curious to find out the best way, and if it's possible to send a specific parameter to an express function in NodeJS. I want to pass the string ('admin') or any other string that I choose to the 'R ...

Enhance link with dynamic content using an AJAX call

I need help appending a picture after a link with the same URL as the link. The current AJAX code is producing the following result: <a href="images/Draadloos.png" data-lightbox="gallerij"></a> Here is an example of what I would like to achie ...

How can we initiate the AJAX request right away while also making sure the page is fully loaded before adding

One trick I've discovered is that by submitting an AJAX request in the <head> section of my webpage, I can avoid a minor flicker on page load when loading content via AJAX. Of course, this method still needs some refining to handle longer AJAX r ...

Hide elements forever once the form is submitted

I'm seeking help to figure out how to make certain elements disappear after a form submission on my website's dashboard page. Specifically, I need to hide three elements once the user has submitted a form. Elements that need to be hidden: .vc_t ...

The setInterval function will run just one time when triggered by an onclick event

After watching a YouTube tutorial on creating desktop notifications, I had an idea to use it as a reminder tool. For example, having a specific reminder appear as a desktop notification every 30 minutes when triggered by clicking a button. However, the cod ...

What is the best way to create TypeScript declarations for both commonjs modules and global variables?

Wanting to make my TypeScript project compatible with both the commonjs module system and globals without modules. I'm considering using webpack for bundling and publishing it into the global namespace, but running into issues with the definitions (.d ...

Issue with exporting to Excel in AngularJS in Internet Explorer not functioning as expected

Having some trouble exporting data to Excel or PDF in IE, but it works fine in Chrome. Since most users will be using Internet Explorer, can someone please review my code and provide suggestions? Below is the Angular function I am using: scope. ...

The server encountered an unexpected error while processing the request, possibly due to a

My JavaScript file includes an interval function that calls the following code: setInterval(function() { $.getJSON('/home/trackUnreadMsgs', function(result) { $.each(result, function(i, field) { var temp = "#messby" + result[i].from; ...

Tips for merging two applications into a single file using Node.js

This code snippet represents the functionality of my chat application. var worker = function(worker) { var http = require('http'); var fs = require('fs'); var app = http.createServer(function(request, response) { f ...

Ensure that each child component receives a prop

Can you transfer a prop with a function to any child component? Specifically, I want to send a function from a parent component to each {children} element: <> <Navbar /> <main>{children}</main> <Footer /> </&g ...

Upgrading from Sequelize V5 to V6: TypeScript bug - Property 'x' is not recognized in type 'y'

Updating from sequelize 5.x.x to 6.x.x has caused some issues for me. In version 5, everything was working fine but after the upgrade, I started facing TypeScript errors with properties generated via associations when trying to use objects from the include ...

Issues with the functionality of the node.js socket.io listener

I am currently experiencing an issue with private messaging in my chat application. The private message functionality does not seem to be working as expected. Below is the code snippet from the app.js file: var express = require('express'),htt ...

Conflict between iOS 7+ swipe back gesture and stateChange animations

When transitioning between states in an AngularJS application, I utilize CSS animations to add visual appeal to the view change. This may involve applying fades or transforms using classes like .ng-enter and .ng-leave. In iOS 7+, users can swipe their fin ...

Encountering the below error message when running the command to initialize expo project:

Dependency installation in progress... Warning: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="accfc3dec981c6dfec9d829e829b">[email protected]</a> is deprecated, consider upgrading to core-js@3 or a ne ...

Developing a dynamic slideshow using jQuery

I'm working on a website where I want an image to change when I click on a specific piece of text. Currently, I have set up a class called "device" with one of them having the class "active" like this: <div class="col-md-3"> <div c ...