Resolving the issue of "Cannot set properties of null (setting 'innerHTML') at HTMLInputElement.loader" involves identifying the root cause and implementing

Need assistance with this code. Although I believe everything is correct, it still isn't working as expected. The error mentioned in the title comes up whenever I try to use it. I've made multiple modifications to the code but it's still not functioning properly. If anyone has any suggestions on how to resolve this issue, please help me out. Thank you:)

let loader = function(e) {
  let file = e.target.files;

  let show = "Selected File: " + file[0].name;

  let output = document.getElementById("selector");
  output.innerHTML = show;
  output.classList.add("active");
};

let fileInput = document.getElementById("file");
fileInput.addEventListener('change', loader);
<input type="file" accept="video/* image/*" multiple name="" id="selector" hidden>
<label for="file" class="upload" id "selector"> Select a file: </label>

Answer №1

Firstly, there is a typo issue: id = "selector" in the <label> (missing "="). Use the example below to correctly select a file:

<input type="file" id="file-selector" multiple>
<script>
  const fileSelector = document.getElementById('file-selector');
  fileSelector.addEventListener('change', (event) => {
    const fileList = event.target.files;
    console.log(fileList);
  });
</script>

The code above will display the name of the file selected next to the button:

To limit the file types:

<input type="file" id="file-selector" accept=".jpg, .jpeg, .png">

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

Text in gray overlaying image background

I am attempting to design a layout that resembles the following: Although I can create the letter "A" using svg or clip-path, my challenge lies in maintaining the background inside the "A" aligned with the body background. Essentially, I want the backgrou ...

The calculator is experiencing issues with JavaScript functionality

Having some trouble developing a calculator using HTML5, CSS, and JavaScript. After passing my HTML and CSS through validators successfully, I encountered issues when adding JavaScript functions to enable the functionality of the buttons on the calculator. ...

Adaptable images - Adjusting image size for various screen dimensions

Currently, my website is built using the framework . I am looking for a solution to make images resize based on different screen sizes, such as iPhones. Can anyone suggest the simplest way to achieve this? I have done some research online but there are t ...

How can you loop through keys and values in a JSON object using JavaScript?

I am attempting to cycle through the JSON data below: { "VERSION" : "2006-10-27.a", "JOBNAME" : "EXEC_", "JOBHOST" : "Test", "LSFQUEUE" : "45", "LSFLIMIT" : "2006-10-27", "NEWUSER" : "3", "NEWGROUP" : "2", "NEWMODUS" : "640" } The keys in this JSON are d ...

The request's body in the PUT method is void

I seem to be having an issue with my PUT request. While all my other requests are functioning properly, the req.body appears to remain empty, causing this error message to occur: errmsg: "'$set' is empty. You must specify a field like so: ...

jQuery UI - Add character right before the final element

My slider is set to range from 10 to 1000 with increments of 20 (10-30-50, etc). I'm looking to add a character before the value on the last step (1000). Currently, it shows values like "560 kr/h" or "400 kr/h", but I want it to display ">1000 kr/h" ...

Guide to dynamically setting a value in an input field using JavaScript's document.querySelector

My goal is to input a value using Javascript. Check out my project link! Click on "add to cart" and then proceed to checkout to reach the checkout page. I am trying to automatically add a Zipcode value to the checkout page. I attempted this method but it ...

Jquery's remove function fails to function correctly when used on a cloned element

I am facing an issue where I have a list of rows that need to be deleted. However, when I attempted to use jQuery's remove function, it only removed the original row and not its clone. My goal is for the parent element of the parent to be removed when ...

Why is my JSON object being mistakenly interpreted as a string literal during iteration?

I'm facing a seemingly simple question but I can't seem to figure it out. Below is the jQuery code I am working with: $(function() { $.get(urlGetContainerNumbers, function(data) { console.log(data); for (var idx = 0; idx &l ...

In Chrome, the $http GET request fails to send the JSESSIONID, but it functions properly on Firefox with AngularJS

Here is the code snippet I am working with: $http({ 'method': 'GET', 'url': 'http://www.example.com', 'withCredentials': true, headers: { 'Content-type': &apo ...

What sets apart the <img> and <Image> layout attributes in NextJS?

While working on custom checkboxes, I came across a unique situation - the only way the positioning of the checkboxes aligns properly is if the classes '.init' and '.hover' are assigned to images, and the class '.done' is disp ...

Having trouble using jQuery's .off() method to remove an event handler?

I'm facing an issue with my jQuery code where the .off('click') method doesn't seem to be working. I've tried removing the event binding from '.reveal-menu', but it's not working as expected. The initial animation wo ...

AJAX Image Upload: How to Transfer File Name to Server?

Has anyone successfully uploaded an image to a web server using AJAX, but struggled with passing the file name and path to the PHP script on the server side? Here is the HTML code along with the JavaScript (ImageUpload01.php) that triggers the PHP: Pleas ...

How can I create a single-page application that adjusts to different screen sizes effortlessly?

I am working on developing a single-page application that fits within one screen with the following structure: There is a fixed header that includes: Two columns (1/3 for left, 2/3 for right) : a logo on the left a breadcrumb on the right. The ...

How can you use the :not selector in CSS to target specific elements within a group?

Consider the following set of inputs: <div id="group"> <input id="uno" class="red"></input><br/> <input id="dos" class="red"></input><br/> <input id="tres" class="blue"></input><br/ ...

Is there a way to position a ListItem (using React) in the lower left corner in this specific case?

import * as React from 'react'; import { styled, useTheme } from '@mui/material/styles'; import Box from '@mui/material/Box'; import MuiDrawer from '@mui/material/Drawer'; import MuiAppBar from '@mui/material/Ap ...

What is the best way to pass an array as a parameter in Angular?

I have set up my routing module like this: const routes: Routes = [ { path: "engineering/:branches", component: BranchesTabsComponent }, { path: "humanities/:branches", component: BranchesTabsComponent }, ]; In the main-contin ...

Do we need to use aria-labelledby if we already have label and input associated with "for" and "id"?

Here is the HTML structure for the Text Field component from Adobe Spectrum: The <label> element has a for attribute and the <input> has an id which allows screen readers to read out the label when the input is focused. So, why is aria-label ...

JavaScript encountered an issue: Uncaught ReferenceError - 'userNumber' is undefined at line 43

I'm currently working on a JavaScript guessing game where I've already set up the necessary functions. However, I keep encountering an error. An error in my JavaScript code is causing a ReferenceError: userNumber is not defined on line 43 to b ...

Is there a way to verify DNSSEC compatibility using node.js code?

Struggling with incorporating DNSSEC compliance checks into my web analytics tools using NodeJS. The standard library's dns module does not seem to accept DNSSEC record types such as rrsig, ds, nsec, and nsec3. Despite checking the documentation, thes ...