I have this control
<div class="conico">
<input class="form-control" readonly id="fileName"/>
<input class="form-control" type="file" style="display:none;" required="xxx" id="fileSelect"/>
<label for="xxx" class="ico">
<img src="/images/icocamera.svg">
</label>
</div>
I want to fill a text box with the name of a file. In my code above, I have an icon for every file. When an icon is pressed, the corresponding file name should populate the input.
This works fine, however, when the user doesn’t select a file, then submits they get an error:
input is not focusable
How can I handle this error with a custom javascript function?
2
Answers
To handle the "input is not focusable" error in JavaScript form validation, you need to understand its cause and implement a solution accordingly. This error usually occurs when you’re trying to set focus on an element that cannot receive it, such as a disabled or hidden input field.
Here’s a concise step-by-step approach to handling this issue:
Identify the source of the error: Examine your code and identify which part triggers the "input is not focusable" message. It could be due to attempting to set focus programmatically using
element.focus()
, or when submitting a form with invalid inputs.Check if the input can receive focus: Before setting focus on an element, ensure it’s enabled and visible by verifying its properties like
disabled
(should be false) anddisplay
/visibility
(shouldn’t be set as ‘none’ or ‘hidden’).Conditionally handle the issue: If an input isn’t focusable due to being disabled or hidden, decide how you want your script to react. You might choose either of these options:
By following these steps and adapting them as per your specific requirements, you can effectively handle the "input is not focusable" situation in your JavaScript form validation. Remember to test thoroughly to ensure your implementation works seamlessly across various scenarios.
fileName needs
required = true
, not the file selector.Then you can do something like this: