Error: Custom button causing issue with 'file' input field

Currently, I am working on developing a custom file field in my AngularJS application. To achieve this, I have created a button using a span element and placed the file input as its child.

However, upon clicking the span (button), I encountered the following error:

Uncaught RangeError: Maximum call stack size exceeded

The main objective is to trigger the click event of the file field when the span is clicked.

Could someone help me resolve this issue?

Below is a snippet of my HTML code (with multiple instances):

<div class="row row3">
    <div class="cell">
        <a ng-href="">Contract Details</a>
        <span class="fileUpload">
            Upload Report 
            <!-- handled by directive through class name  -->
            <input 
            info="contractor.Id" 
            upload="uploadFile" 
            class="uploadField upload-file-directive" 
            type="file" />
        </span>
    </div>
</div>

CSS styling (focusing on functionality):

.fileUpload .uploadField

border : 1px solid red;
position : absolute;
left : -200em;

My current Directive:

// File upload functionality implemented here

var uploadFileDirective = function () {

    return {

        replace: false,

        restrict : 'C',

        scope : {

            info:"=",
            upload:"="

        },

        link:function ( scope, element, attrs ) {

            var button = element.parent('.fileUpload'); // selecting parent

            button.on( 'click', function () {

                $(this).children('.uploadField').click(); // triggering

            });

            element.on('change', function ( event ) { // change event handling

                var files = event.target.files;
                scope.upload(files, scope.info );

            });
        }

    }

}

angular.module("tcpApp")
.directive("uploadFileDirective", uploadFileDirective);

Despite the efforts made, I continue to face the mentioned error. Can anyone suggest the correct approach to creating a custom file field?

Answer №1

This snippet showcases the code I crafted for my live app, thoroughly tested and running smoothly.

<div class="file-input-name">{{featuresFile.name || 'NO FILE SELECTED'}}</div>
<label class="md-primary md-raised md-button" md-ink-ripple for="file-input-fts">
    <span>Select</span>
</label>
<input id="file-input-fts" type="file" file-model="featuresFile">
<md-button class="md-raised md-primary" ng-click="upload.uploadCsvFeatures()">UPLOAD</md-button>

For Angular Material usage in my app, I opted for md-button, but standard HTML <button> tags work just as well.

To summarize, concealing the file input is key, utilizing the label as a button for browsing files.

The CSS snippet includes:

#file-input-fts {
    display: none;
}

Note that classes starting with md-* are specific to Angular Material for styling purposes, easily customizable or replaceable with your own styles or libraries.

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

Tips for updating button dimensions and colors on the fly in React

Currently, I have developed a custom Button component in my React application. At the moment, I am able to dynamically modify the color by specifying it within the color attribute. However, I now aim to provide an option to change the size of the button as ...

The inner div is too big for the outer div to contain

I am facing an issue with three div elements, where the first (content) contains two nested div elements. While in Chrome, the last two div elements fit perfectly inside the first div, in MS Edge, the last div does not and starts after the first div on th ...

Creating a countdown timer and looping through the seconds using HTML and JavaScript

I tried to incorporate a countdown on my website, but I'm facing an issue where the seconds freeze instead of continuously running. Here's the code snippet I used: <span id="dhour"></span> h <span id="dmin"></span> min & ...

Implementing translation functionality within an AngularJs controller

Utilizing angular translate for translating certain words in the controller, with translation stored in json files within the html. // LABELS var monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "Septe ...

Does anyone have any insights into the technology that powers Twiddla's browsing features?

After some observation, I came to understand that simply inserting <iframe src=remote-page-here></iframe> will not work as expected. Interestingly, the website twiddla does not use flash either. I am intrigued by how they achieve the in-place b ...

The performance of Jquery Animate is acting strangely after the upgrade to version 1.11

Take a look at http://jsfiddle.net/integerz/k19x8L1b/2/ which uses version 1.7.x $(function () { $("#clickme").toggle(function () { $(this).parent().animate({right:'0px'}); }, function () { $(this).parent().animate({righ ...

Interactive canvas artwork featuring particles

Just came across this interesting demo at . Any ideas on how to draw PNG images on a canvas along with other objects? I know it's not a pressing issue, but I'm curious to learn more. ...

Error in parsing the HTML code

However, the issue persists on my website located at The following error message keeps appearing: HTML Parsing Error: Unable to modify the parent container element before ...

Export testing results from Protractor, AngularJS, and Jasmine to an XML file

Looking to export protractor results to xml files, I stumbled upon this helpful resource online: https://github.com/angular/protractor/issues/60 Upon executing: npm install jasmine-reporters I inserted the following lines into my protracotr config file: ...

Struggling to figure out why BXSlider won't cooperate

I'm struggling to make BXSlider work on my website. Here is the code I have used. The JS/CSS files are properly linked and downloaded from the site with correct paths. $(document).ready(function(){ $(document).ready(function(){ $('.bx ...

The CSS 'top' attribute is without impact

As I begin my journey with CSS, I am eager to grasp some of its behaviors. In the CSS file, the following code is defined: #spa { position : absolute; top : 8px; left : 8px; bottom : 8px; right : 8px; min-height : 500px; min-width : 500px ...

In the absence of an Avatar, verify the gender from the JSON data and insert it into the image

I have created my very first app where users can upload their avatars and set their gender. If a user (in this case, a girl) does not upload an avatar, I want to check the JSON data for their gender information. Based on this gender information, I want to ...

I'm looking for assistance on how to set the minimum height using jQuery. Can anyone provide

My attempt to use the minHeight property in one of my divs is not successful, and I am unsure why... <div id="countries"> <div class="fixed"> <div class="country" style="marging-left:0px;"></div> <div class="country"&g ...

Email Plugin for PhoneGap

I am currently exploring the capabilities of the plugin here. This is my first time working with Phonegap in Android, so the whole process is a bit new to me. Up to this point, I have added the .JAVA file to the "src.org.apache.cordova" directory, include ...

Utilizing the column approach within Protractor

I've been going through the documentation trying to figure out exactly what the column method does, but I can't seem to find a clear explanation. Can anyone shed some light on its functionality? Here's the code I'm working with: var p ...

Tips on boosting the speed and user-friendliness of your AJAX/REST application

Exploring the Essential Requirements of an Application I am currently developing a comprehensive application using AngularJS (but it could be any framework). This application interacts with a server built on Google App Engine through REST technology. Th ...

Understanding the behaviour of injecting attributes in directive scopes

Can anyone provide some insight on why my JavaScript key code isn't working as expected in the second directive of my example below? It seems that injecting scope attributes is causing the function passed into the directive not to be evaluated properl ...

What could be causing the issue with absolute positioning not functioning correctly?

I'm having trouble keeping my footer at the bottom of the page: body, html{position:relative;} footer{position:absolute;} Even when I use bottom: 0;, the footer doesn't seem to go all the way to the bottom. Is there anyone who can help me troub ...

Tips for using multiple Angular directive modules in PprodWant to enhance your Pprod experience by

Currently, I am working on jhipster Release 0.7.0 and our jhipster app has multiple types of directive modules – one for the index page and another for common directives. However, when we run the app on Prod profile, an exception occurs: [31mPhantomJ ...

Ensuring column content is perfectly aligned using Bootstrap 4

I'm attempting to use bootstrap to align rows and columns in my html so that it resembles the layout shown in this image.... https://i.sstatic.net/KC0sa.png I experimented with code like this... <div class="container"> <div class="row"> ...