How can I trigger a select file dialog on iOS 6 for an input element in the DOM programatically? (i.e. using Javascript)
<input type="file" id="foo">
Notes:
Mobile Safari and Safari seems to behave differently. I've got it working using document.querySelector("input[type=file]").click() in safari.
If it's within a native clickhandler, i.e. initiated by the user, it seems to work, but I'd like to trigger the dialog on will.
After some more research it seems like this isn't possible. Least I haven't found a method that works.
I decided to solve this using an <input type='file'> positioned on top of the button, with opacity 0.01 to make the button visible.
Related
I am developing a WebApp and want to create a kind of login screen with html. For this I have four input fields, which focus on the next field after a number has been typed in.
In osX safari this works without any problems. Unlike iOS on my iPhone. Yoo-hoo.
The problem on mobile safari is that I don’t know how to automatically "jump" to the next input field programatically (set the focus programmately). The user can do it via a button (working fine), but is there a way to do this automatically?
See my code:
$('.text').on('input', function() {
$(this).next('input').focus()
});
.text {
margin: 5%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="text" />
<input type="text" class="text" />
<input type="text" class="text" />
<input type="text" class="text" />
The focus is working quite fine on webBrowsers like safari or chrome but not for iPhone iOS safari like to can see in this gif - The focus get's lost and the keyboard disappears:
What I've found during my research was e.g this question:
Programmatically focus on next input field in mobile safari
Mobile Safari: Javascript focus() method on inputfield only works with click?
But none of the answers did the job for me because whether they are not working / they are to jerky or they use a workaround that is not really working with my styles like http://jsfiddle.net/EbU6a/194/ did.
So any help how to fix this would be really appreciated. Thanks in advance!
Since I am pretty sure that someone will get this problem too, I decided to answer my own question.
So I've fixed the issue partly using this line of code:
myWebView.keyboardDisplayRequiresUserAction = false
I found the solution while reading the apple documentation for UIWebView.
Please see this reference.
I am affraid iOS does not allow you to focus programatically. You can try workaround from here
I have a simple 'Cancel' button with the same tooltip (title) value, which closes using kendo method, inside Kendo popup. It works fine except in Microsoft Edge Browser, when touched. Post touch, the tooltip value doesn't goes away unless clicked anywhere else on the screen.
<input type="button" value="Cancel" title="Cancel" onclick="javascript:closeWindow();">
Microsoft Edge doesn't support touch events by default. It has an alternative system called pointer events. Sometimes 3rd party libs implement touch based widgets that don't play well with pointers. A quick way to determine if this is the case is to switch on touch events inside of Edge. Put about:flags in the address bar then go to the setting enable touch event and change it to always.
If the site now works, then I suspect it's an issue with the library. If that is the issue then I'd raise the issue with Telerik (the folk behind kendo) on their forums, they can probably help identify the issue specifically so that it can be fixed in the library.
<input type="file"> in IE6 when I write something in field text area it is writable but not in firefox and chrome. I want to open browser window in IE6 on clicking on text field area as same as Firefox.
The file input field's look and feel and behabior are different from browser to browser. It's not easy to change this. You are better off using a plugin that hides the file input field and replaces it with a custom one. Like the one here - http://plugins.jquery.com/project/jquery-prettyfile.
With jQuery you may also try the following code -
$('input[type="file"]').focus(function(){$(this).click();$('body').focus();});
I don't have IE6 so can't check it; but it works on IE7 and 8.
This is basically and simplified what I have now:
<style>
form.noshow { height: 0; overflow: hidden; }
</style>
<form class=noshow target="SomeIframeThatExists">
<input type=file id=uf>
</form>
<a id=uflink href="/user/photo">Upload photo</a>
<script>
$('uf').addEvent('change', function(e) {
// alert('oele'); // this would work fine
this.form.submit(); // auch in IE > "Access denied" exception
});
$('uflink').addEvent('click', function(e) {
$('uf').click(); // opens file dialog in all browsers inc IE
return false;
});
</script>
What it does (perfectly) in Chrome 11 and FF 4:
The form is hidden.
You click the link
Choose file dialog opens
You select a file
Dialog closes
Form is submitted
Script in iframe is executed and photo is replaced
Very hightechlike and sweet.
In IE, all of that works, except [6]. The form isn't submitted. Javascript error: "Access denied". Doesn't matter how the form is invisible, as long as the dialog was opened with input.click() the form can't be submitted on change. (The onchange function is executed fine. The error only occurs when form.submit() is called.)
Now all of this I can accept. IE sucks. I live with it.
My solution so far was to check the navigator for "MSIE" and then when clicking the link instead of opening the dialog, showing the form (with the file input). Then the user has to click the actual, ugly file input and then everything works fine. But ugly.
The question is twofold:
Is there a way to do this in IE as cool as it is in Chrome? WITHOUT nasty flash/java crap. Only html elements andjavascript.
If not: is there a way to check for form.submit() support after opening the dialog from a link (besides !navigator.contains("MSIE"))?
[2] could be catching the "Access denied" exception thrown in IE, but then it's too late: the user has already opened the dialog and browsed to the photo. You don't wanna make him do that again. (Even IE users don't deserve that.)
PS. I'm only interested in Chrome 10+, Firefox 3.6+ and IE8+.
PS. Might be important: the file input element can't be anywhere near the link, because the link is inside a form and that form is (must be) separate from the file upload form.
UPDATE
Second best: detect support for this high-techlike behaviour that only doesn't work in IE. I don't wanna use navigator.appName.contains('MSIE') because that's not flexible and not necessarily true.
#Rudie, up here - Thanks for that code! It works great in IE and Chrome, but not in FireFox.
I managed to take my old code (That works in FF and Chrome) and combined your code for MSIE.
Check it out here:
FIX FOR IE, CHROME AND FIREFOX
https://gist.github.com/4337047
PROBLEM:
When an file-input is opened via a scripted, forced click() event, IE won't let you submit the form.
If you click the file-input via your own mouse (what we don't want), IE will let you submit the form.
Please note that IE11, as of now, is allowing the form to submit if a file input field has changed via a scripted 'click' event.
Solution
(partly thanks to Rudie # Stackoverflow, https://stackoverflow.com/users/247372/rudie , http://hotblocks.nl/):
Make a label for the input in IE. If you click it, it will force a click on the input field - and IE will accept that (dumbass IE thinks user clicked the input field, hah)
So in that label we'll put our own styled DIV.
Next problem was, this doesn't work in FF. So we made a simple (possible nasty) browser check, and based on the browser we'll show a different button.
Solution is right here. Tested in:
Win 7 x64
FireFox 13.01
Chrome 23.0.1271.97 m
IE9 in regular IE9 mode
More tests / additions to code are MORE than welcome!
EDIT:
To quote Roy McKenzie
IE11 is now allowing the form to submit if a file input field has changed via a scripted 'click' event.
I did it!!
http://jsfiddle.net/rudiedirkx/8hzjP/show/
<label for="picture">Upload picture</label>
<input type="file" id="picture" style="position: absolute; visibility: hidden" />
IE8 works. I don't care about others.
So easy =)
Strange indeed, IE8 block the submission only if there's enctype="multipart/form-data" in the form.
One workaround that worked for me locally is adding "real" submit button e.g.
<input type="submit" id="btnSubmit" value="Submit" />
Then have such code to submit:
$('btnSubmit').click();
If this works you can probably hide the button with CSS to make it transparent to the user.
Well, this is the EXACT same problem I'm having right now. And only (disgusting) hack that did solve is to make the input[type=file] quite big with CSS, make it alpha=0 and put it on top of your intended UI element.
In short, you're making the user click the ugly "browse" button without him/her realizing.
Try adding the tag enctype="multipart/form-data" to your form element.
we have an issue where a disabled form element (input, textarea, ...) does not react to a right-click action (oncontextmenu attribute). Everything works fine when the form element is enabled. Please consider the following pseudo-code:
<div id="test" oncontextmenu="someFunction()">
<input id="textbox" type="text" disabled="disabled">
SOME_PADDING
<input id="calendar" type="image" disabled="disabled">
</div>
The real catch is it does not work in Chrome and Firefox, but it does work in IE8 and Opera.
When we right-click on the textbox or calendar elements in Chrome or Firefox, nothing happens. If we click in between the elements (SOME_PADDING) then the right-click menu does appear.
Hence it seems as if a right-click action on a disabled form does not work in Chrome and Firefox. Has anyone experienced a similar behaviour before?
Thanks in advance!
Stijn
EDIT: As mentioned by Pekka, a disabled form element not reacting to a right-click does make sense. The real issue seems to be the oncontextmenu attribute of the div around it not reacting properly when clicking on a disabled form element in Firefox / Chrome.
EDIT2: Online example can be found here: http://jsbin.com/isite4/6 - This works in all but IE8. Any proposed work-arounds to force IE to behave normally?
Hmmmm, interesting. Never seen this before. It could be argued that it is not entirely wrong to hide the context menu, though. The W3C has the following to say about disabled controls:
Disabled controls do not receive focus.
Disabled controls are skipped in tabbing navigation.
Disabled controls cannot be successful.
and about receiving focus:
In an HTML document, an element must receive focus from the user in order to become active and perform its tasks.
So the behaviour shown by Chrome and FF kind of makes sense IMO.
Ideas for how to work around it:
Put a transparent element on top of the input element, and catch the event there (yuck)
Use z-index: -1 to put the disabled element behind the container - not sure whether this works across browsers though
Do not work with the disabled attribute, but use CSS styling and a jQuery workaround to prevent those controls' values from being submitted
The latter would be the best suggestion, I think.
We opted for a work-around, as their doesn't seem to be a straight-forward approach.
We decided to add an image over the disabled form element, that responds to a normal left-click.
There is a simpler way that I have used and that was to readonly the element instead of disabling it. If the 'look' might confuse your users, just match the "disabled" colours with css on the element. I know it is not relevant to the original poster but anyone else who comes looking might benefit.