I'm creating an AJAX form which includes the option to either specify a background color or upload a background image. The goal is for the bgColor
field to be ignored if a file has been specified for the bgImg
field.
<label>Color: <input type="color" name="bgColor" value="#000000"></label><br>
<label>Image: <input type="file" name="bgImg" accept="image/png"></label><br>
I figured the easiest way to collect the form data is, of course, using the FormData API:
var fd = new FormData(document.getElementById('myForm'));
The problem is, I don't know how to check the FormData object for whether or not a file has been selected. Whether or not the file input is empty, fd.has('bgImg')
returns true because the field is present--okay, that's sensible.
But although fd.get('bgImg')
works fine if a file has been specified, and I can then verify the positive case, when the file input is empty that same line straight up crashes my browser! (I've only checked in Firefox, but it happens consistently whether in my actual script or from the browser console.) Unfortunately a "check whether there's a file" operation that is recognizable but undecidable is effectively useless. So how am I supposed to figure out if the bgImg
field is empty?
I realize I can also just check the form's elements['bgImg'].files
object, but the FormData API is already there, and it's neater, and it would be easier if it weren't apparently fatally borked! So am I missing something? If this is somehow the wrong way to use the putatively convenient FormData object, then what the hell am I supposed to be doing instead? Is checking the files
collection actually the only solution?
EDIT: Further investigation reveals that this API has pretty poor support in browsers other than Firefox, so actually checking element.files
is probably the better solution. I'm still baffled as to why this would be crashing the browser in Firefox, though. If an answer on that front is not shortly forthcoming, I'll probably submit my own answer.
The get() method of the FormData interface returns the first value associated with a given key from within a FormData object. If you expect multiple values and want all of them, use the getAll() method instead.
Answer: Use the jQuery val() Method You can use the val() method to test or check if inputs are empty in jQuery.
Returns an iterator iterates through all key/value pairs contained in the FormData . FormData.get() Returns the first value associated with a given key from within a FormData object. FormData.getAll() Returns an array of all the values associated with a given key from within a FormData .
This behavior of the FormData API in Firefox seems like it may be a bug, unfortunately. However, given the rather minimal support for the FormData methods across browsers, the better solution is probably to check the form elements anyway:
var formFields = document.getElementById('mandelForm').elements;
console.log(formFields['bgImg'].files.length > 0); // True if file selected
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With