Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

99
Views
Javascript code refactor to use in Stimulus controller

How to refactor JS code like this:

const fileInput = document.getElementById('input');
const p = document.querySelector('p');
fileInput.onchange = () => {
  const selectedFiles = [...fileInput.files];
  p.textContent = selectedFiles.map(s => s.size).join(', ');
}

to make it work in rail's Stimulus? I have very little experience with JS and want to have one or two examples of implementing JS code in Stimulus controllers, so I have material to refactor other cases on my own. Much thanks.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

One possible solution:

<div data-controller="file">
  <input type="file" data-file-target="input" data-action="change->file#displayText" id="my-input">
  <output data-file-target="text" for="my-input"></output>
</div>
// filename_controller.js

// Connects to data-controller="file"
export default class extends Controller {
  static targets = ["input", "text"]
  
  displayText() {
    const selectedFiles = [...thisInputTarget.files];
    this.textTarget.textContent = selectedFiles.map(s => s.size).join(', ');
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!