When I press the button, the text box fails to refresh

Here is an example I want to share with you: code demonstration.

Below is the HTML code snippet:

<input id="uploadFile" placeholder="Choose File" disabled="disabled" />
<div class="file-upload btn btn-primary">
    <span>Upload</span> 
    <br> 
    <input id="uploadBtn" class="upload" type="file">
</div>

The CSS code looks like this:

.btn {
    border: 1px solid transparent;
    border-radius: 4px;
    cursor: pointer;
    display: inline-block;
    font-size: 14px;
    font-weight: normal;
    line-height: 1.42857;
    margin-bottom: 0;
    padding: 6px 12px;
    text-align: center;
    vertical-align: middle;
    white-space: nowrap;
}

.file-upload input.upload {
    cursor: pointer;
    font-size: 20px;
    margin: 0;
    opacity: 0;
    padding: 0;
    position: absolute;
    right: 0;
    top: 0;
}

.file-upload {
    margin-left: 10px;
    overflow: hidden;
    position: relative;
}
.btn-primary {
    background-color: #428bca;
    border-color: #357ebd;
    color: #fff;
}

And here is the JavaScript code part:

document.getElementById("uploadBtn").onchange = function () {
    document.getElementById("uploadFile").value = this.value;
};

While testing this on JSFiddle, the textbox updates when the upload button is clicked, but when I try it in Firefox, the textbox doesn't get updated.

I found some helpful tips on How to Style a HTML file upload button using Pure CSS

Any suggestions or solutions for this issue?

Answer №1

It appears that your JavaScript code may not be functioning properly due to the fact that the element with the ID of uploadBtn has not been created at the time you are attempting to assign an onchange event listener. One solution could be to ensure that the window has fully loaded before executing this code.

window.onload = function() {
   document.getElementById("uploadBtn").onchange = function () {
      document.getElementById("uploadFile").value = this.value;
   };
};

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

How to make Angular 5 wait for promises to finish executing in a for loop

My task involves working with an array like this: arr = ['res1', 'res2', 'res3']; For each value in the arr, I need to make an API call that returns a promise. arr.forEach(val => this.getPromise(val)); The getPromise me ...

Tips for Resizing a Div While Maintaining Aspect Ratio

I'm trying to create a div with a background image size of 1170px x 929px that will maintain its aspect ratio when the width is resized. Can someone explain how to achieve this using CSS? Below is the code I have so far: <section class="box"> ...

The exported class does not properly recognize the input values, causing them to be displayed as Undefined and triggering an error

Currently working with Ionic Framework version 6.13.1, I have two services set up in a similar code structure. One is the appointment.service.ts, and the other is the registration.service.ts. Each service corresponds to its own TypeScript file containing a ...

Transforming content dynamically

I am currently working on a single-page website that features a comprehensive product catalog. Here are the key elements I'm focusing on: A one-page website layout, complete with header, content, and footer (developed using HTML5/CSS3) Dynamic cont ...

Creating a unique carousel design using only CSS in Bootstrap

Trying to create a CSS effect to enhance Bootstrap, I noticed that it only works correctly when clicking the right arrow. Whenever I click the left one, nothing happens. I'm not sure where I made a mistake, can anyone help me spot it? Thanks in advanc ...

Firebase scheduled function continues to encounter a persistent issue with an "UNAUTHENTICATED" error being consistently thrown

I have created a firebase-function that is scheduled to retrieve data from an external API source and save it in Firestore. const functions = require("firebase-functions"); const admin = require("firebase-admin"); const { default: Axios ...

Distinguishing between `notEmpty` and `exists` in express-validator: what sets them apart

I'm confused about the distinction between exists and notEmpty in express-validator as they seem to function identically. ...

What is the solution for the error message "TypeError: app.use() is seeking a middleware function"?

I am a beginner in Node.js and have encountered an issue in my passport.js or signupLogin.js file with the error message, app.use() requires a middleware function that I am struggling to resolve. I suspect it may be related to the signupLogin route, as th ...

NextJS Input Field causing a strange "Hydration Error indicating server HTML should not contain a <div> within a <form>", perplexingly occurring only in Chrome and exclusively on my personal computer

Looking for some assistance with a website I'm working on using NextJS and TailwindCSS. There seems to be a pesky little issue in my code that's causing some trouble. Below is the code snippet for the MainContent section of the page: "use c ...

Encountered a problem while trying to upload a video on bunny stream using node.js

Having trouble uploading videos to the Bunny Stream API using Node.js and Axios. Everything else seems to be working fine, like fetching, deleting, changing names, and resolutions of videos. However, when trying to upload a video, consistently receiving 40 ...

Using nextjs9 to render .svg files within an img tag

Currently working on upgrading a Migration project from Nextjs7 to Nextjs9, and encountering difficulties when trying to include .svg files in the image tag. If you'd like to see a DEMO of this issue, please visit: https://codesandbox.io/s/nextjs-mh9 ...

Unable to use innerHTML function on Blogger platform

I am currently working on a basic voting system. It functions perfectly when the two files are in the same location (locally). However, once I publish it on my blogger platform, the system fails to display the results. (When a user clicks to vote, it regi ...

Exploring JSON objects within other JSON objects

As I work on my Angular App and implement angular translate for dual language support, I've encountered an issue with accessing nested items within my JSON object. After carefully constructing my JSON and verifying its validity, I attempt to retrieve ...

Get the value of a function that was previously assigned to an onclick event using JavaScript

Here is the code snippet I am currently working with: document.getElementById(myid).onclick = function() { HandleGPIO(val1, val2); }; if (console) { console.log(document.getElementById(myid).getAttribute('onclick')); } My goal is to de ...

Troubleshooting a Windows Phone HTML5 app encountering an Ajax error

Currently, I am working on developing a Windows Phone application specifically focusing on creating a "Windows Phone HTML 5 app". However, I have encountered an issue while using ajax with Jquery (version 1.9.1). The snippet of my code is as follows: ...

What's the best way to add a timestamp and class name to Angular's $log for an enhanced logging experience?

Is there a way to customize Angular logs by adding a timestamp and classname? For example: $log.info('this log entry came from FooBar'); "9:37:18 pm, FooBar: this log entry came from FooBar" I've come across various examples online that ...

Troubleshooting: Button Functionality Issue in HTML and Javascript

Currently, I am in the process of completing my final project for an introductory web development course. While I am very new to javascript and html, the project itself is a simple Mad Libs task. Throughout the day, I have encountered numerous issues attem ...

When utilizing Solidity Js, upon invoking the function 'get', I encounter an issue displaying the error: "TypeError: Data location must be 'memory' or 'calldata' for return parameter in function"

` //SPDX-License-Identifier: MIT pragma solidity ^0.8.0; contract MyContract{ string value; constructor() public { value = "MyValue"; } function get() public returns(string) { return value; } function set(string memory _value) publ ...

What is the best way to create a smooth transition between multiple cameras or scenes in three.js?

Wondering if it's feasible, and if so, what's the optimal method to create a specific effect in three.js: I have scene S1 being shown from camera C1and want to: transition to scene S2 as seen from camera C2, or transition to scene S1 as viewed ...

Select elements to apply styles to all child elements except the initial one

In the following HTML snippet, we have a div#parent with multiple child elements: <div id="parent"> <div id="ch1"></div> <div id="ch2"></div> <div id="ch3"></div> .... <div id="ch1234">&l ...