How can I conceal the upload button and still display the names of the files?

I created a user-friendly drag and drop zone for uploading multiple files. Users can simply drag their files into the designated drop area and see the file names displayed there. However, I encountered an issue where hiding the standard "Choose file" button using CSS also hides the file names. As a beginner, I'm seeking a simple solution to fix this problem. Thank you!

HTML:

         <div class="drop-area">
            <span class= "drop-area__prompt">Drag Files Here</span>
            <input type="file" name="myFile" class= "drop-area__input">
        </div>

CSS:

.drop-area__input{ 
    display:none;
}

Answer №1

It appears to me that utilizing JS is necessary in order to achieve this task. In the example I provided, file upload is initiated by clicking on an emoji. In your situation, you will need to incorporate it into the method that handles drag and drop.

let fileUpload = document.getElementById('fileUpload');
let fileName = document.getElementById('fileName');

fileUpload.addEventListener('change', function(){
    if(this.files.length)
        fileName.innerText = this.files[0].name;
    else
        fileName.innerText = '';
});
<div class="drop-area">
            <span class= "drop-area__prompt">Drag files here</span>
            <span id="fileName"></span>
            <label>
                    <img height="30" src="https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/240/apple/237/outbox-tray_1f4e4.png"/>
                    <input id="fileUpload" type="file" name="myFile" class= "drop-area__input" style="display:none;">
            </label>
</div>

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

Looking for assistance with implementing touch gestures on an HTML website?

I am in the process of developing a web application, and I have implemented a menu bar that shifts when a user clicks on an arrow. Each click activates a new menu option. Currently, I am looking to provide users with the ability to swipe over the screen t ...

How to efficiently eliminate redundant entries from an object array using JavaScript or React

I've been struggling to implement the solutions provided in this thread on removing duplicates from an array of objects. Unfortunately, I haven't found a satisfactory way to do so. In my current approach (which involves adding new iconsProps to ...

Is there a way to obtain the Base64 data following the conversion of a div to an image?

I am not very experienced with JavaScript, but I wanted to convert a div to an image. I came across this helpful jsfiddle that showed me how to do it. $(function() { $("#btnSave").click(function() { html2canvas($("#widget"), { on ...

Error encountered: Local HTML file - XMLRequest: Access denied due to network error 0x80070005

Update: My issue is not a duplicate, as it persists across all browsers such as Edge, Chrome, and Firefox. I am working with an HTML file that will reside on the user's local machine, rather than being accessed through an HTTP protocol. It is crucial ...

Retrieving a String from a Node.js Server using an Ajax POST call

This is my debut project experimenting with ajax and node.js and up until now, all my POST requests have been executed through form submissions. Currently, I am delving into using $.ajax to initiate a POST action and transmit the string 'cocktailid&a ...

What is the method for specifying the default option in a SELECT Tag and how can the Index of the chosen option be obtained?

Looking for HTML and JavaScript code that can manipulate SELECT and OPTION Elements. <select> <option> </option> <option> </option> </select> How do I retrieve the index of the selected option and obtai ...

Using jQuery to extract the value of an object from an <li> element and updating the class attribute of the <li> element

There is a div element: <div id="ddDistr" class="searchBoxSortDistrict" tabindex="1"> <ul id="ddd"> </ul> </div> I have inserted HTML from json into it: $("#ddd").html(" "), $.each(dis, function( index, value) { $("#dd ...

Unable to collapse the Bootstrap dropdown menu

For the life of me, I can't seem to find a solution to my problem. I'm currently working on creating a responsive navbar for my website by following a tutorial. The goal is to make it mobile-friendly with a button that expands the menu on smaller ...

CSS shimmer effect does not smoothly transition between borders

Currently, I'm diving into a tutorial on YouTube that teaches how to craft a CSS Glowing Border Animation After putting in the effort to replicate the animation, I noticed a glitch that's been bothering me. It seems like there's an abrupt s ...

Having difficulties redirecting with the button's onclick function

I'm struggling to redirect users to another page using a button (assuming the hostname is localhost:8000) $('#getFruit').attr('onclick', window.location.host + '/getFruit/banana') <script src="https://cdnjs.cloudfl ...

Turn off the standard sign in prompt when utilizing the NTLM pass-through method in express-ntlm

In my node.js app, I am utilizing the following code with express-ntlm to obtain workstation details: var express = require('express'), ntlm = require('express-ntlm'); var app = express(); app.use(ntlm()); app.all('*', func ...

Tips for identifying repeating id numbers within a form?

I recently completed a form with approximately 800 fields, only to realize that I accidentally gave some of the fields the same ID. Can someone please advise me on how to track down these duplicate IDs? ...

How can I extract the URL from the event listener in Cordova's in-app browser event and then automatically close it once a specific URL is reached?

In my journey with ionic version 1 (just starting out with ionic & angularjs), I encountered an issue while trying to close the in app browser upon reaching a specific URL. The problem arises from the fact that the event triggered on "loadstart" is o ...

What is the best way to pass multiple variables to a PHP file with AJAX using a GET request?

Can you help me figure out how to send both an I.D. value and a name value to a php file using ajax? Currently, I am successfully sending just the I.D. variable, however, when I attempt to add the name variable, the function stops working. The code that w ...

In Angular's production build on Webkit/Safari, the left-hand side of the operator '=' must be a reference

Recently, I completed a project using Angular. After successfully building it for production without any errors, everything seemed to work perfectly on Chrome. However, when attempting to run the app on Webkit/Safari, an error was displayed in the cons ...

Please add color to the space beneath the line in a highchart

I have been tasked with customizing a line chart that was not originally created by me. The application pulls data from a database and organizes each element by hour before displaying it on the chart. When you hover over a point, the interface looks like t ...

Using Bootstrap 5 to beautifully wrap an even quantity of boxes

My challenge is to optimize the spacing between 4 boxes based on screen size: The layout should adjust as follows: If it's xxl or larger, all 4 boxes should be in a single horizontal row. If it's md or larger, two boxes should be in the firs ...

Developing a basic program that includes iteration, and noticing a small issue that can likely be easily corrected

I am currently working on a straightforward application that prompts the user to specify the maximum number for iteration and the number of rows they want to see as output. Although I believe I'm almost there, I'm still relatively new to this, so ...

What is the best way to toggle the display of sibling spans using SCSS?

Looking at this design:https://i.sstatic.net/DGFjE.png In order to achieve the desired outcome of hiding the 'magnifier' icon and showing the 'close' X icon when the input's placeholder is not visible, I have the following markup: ...

Tips on invoking a method from a JavaScript object within an AJAX request

Considering the following code snippet: var submit = { send:function (form_id) { var url = $(form_id).attr("action"); $.ajax({ type: "POST", url: url, data: $(form_id).serialize(), dataType: 'json', succes ...