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

258
Views
Not being able to call a javascript function through a button

So I have this program where I update a .txt file and it shows me the content of it. But now I want it to show me only when I click on the button. But it´s not woking.


<div id="output"></div>
<button id="btn">Click me</button>

  <script type="text/javascript">
    document.getElementById("btn").addEventListener("click", function () {
        const input = document.getElementById("inputfile");
        if (!input.files.length) {
            alert("No file selected!");
            return;
        }

        const fr = new FileReader();
        fr.onload = function () {
            if (!fr.result) { 
                return;
            }
            const resultAsArray = fr.result.split('\r\n'); 
            let index = 0; 
            const displayInterval = setInterval(() => { 
                if (index === (resultAsArray.length - 1)) { 
                    clearInterval(displayInterval); 
                    return;
                }
                let html$ = document.getElementById('output').innerHTML;
                html$ += '<div>' + resultAsArray[index] + '</div>';
                document.getElementById('output').innerHTML = html$; 
                index++; 
                
            }, 1000);

        }

        fr.readAsText(this.files[0]);

    });
</script>
    

I got this javascript code from the Internet

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

0

Try doing this:

Wherever you're doing the HTML Stuff:

<button id="button-id">Click Me</button>

Then, inside your JavaScript:

let button = document.getElementById('button-id')
button.addEventListener("click", theFunction);

function theFunction() {
//Code to be executed when the button is pressed..
}
about 4 years ago · Juan Pablo Isaza Report

0

Give an id to button then use this code.

    document.getElementById('buttonID')
        .addEventListener('click', function (){
   //statements
})
about 4 years ago · Juan Pablo Isaza Report

0

You can name the function, then change to get files from input, so just replace this.files[0] to file than you get from input.

<input type="file" name="inputfile" id="inputfile">
<br>

<div id="output"></div>
<button onclick="test()">Click me</button>


<script type="text/javascript">
    function test() {
        var inputElement = document.getElementById('inputfile');
        var fileList = inputElement.files;
        var fr = new FileReader();
            fr.onload = function () {
                if (!fr.result) { 
                    return;
                }
                const resultAsArray = fr.result.split('\r\n'); 
                let index = 0; 
                const displayInterval = setInterval(() => { 
                    if (index === (resultAsArray.length - 1)) {
                        clearInterval(displayInterval); 
                        return;
                    }
                    let html$ = document.getElementById('output').innerHTML;
                    html$ += '<div>' + resultAsArray[index] + '</div>'; 
                    document.getElementById('output').innerHTML = html$; 
                    index++; 

                }, 1000);

            }
            fr.readAsText(fileList[0]);
    }
</script>

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!