2

Strange situation:

on my plugins.js file I have that simple function:

 function getFile(id) {
     document.getElementById("fileUploadHidden+" + id).click();
 }

It works perfectly on web browsers - even on WindowsPhone, iPhone/iPad.. but not on android. It looks like android browser ignores .click() function.

Why? And how to resolve it?

whoah
  • 4,363
  • 10
  • 51
  • 81
  • can you post what you get in the variable `id` and your Html too. – Shaunak D Jun 13 '14 at 12:16
  • Check out this answer: http://stackoverflow.com/questions/2381572/how-can-i-trigger-a-javascript-event-click/38073679#answer-38073679 – Manolo Jun 28 '16 at 10:49

5 Answers5

3

I think you should add the touchstart:

var isMobile = {
   Android: function() {
      return /Android/i.test(navigator.userAgent);
   },
   BlackBerry: function() {
      return /BlackBerry/i.test(navigator.userAgent);
   },
   iOS: function() {
      return /iPhone|iPad|iPod/i.test(navigator.userAgent);
   },
   Windows: function() {
      return /IEMobile/i.test(navigator.userAgent);
   },
   any: function() {
      return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Windows());
   }
};

if(isMobile.any){
   document.getElementById("fileUploadHidden+" + id).trigger('touchstart');
}else{
   document.getElementById("fileUploadHidden+" + id).trigger('click');
}
Jai
  • 74,255
  • 12
  • 74
  • 103
0

If id name is fileUploadHidden

Try

$("#fileUploadHidden" + id).click();

If id name is fileUploadHidden+

Try

$("#fileUploadHidden\\+"+id).click();
Sridhar R
  • 20,190
  • 6
  • 38
  • 35
0

I never coded for android browsers, but I research such problems first with alerts to see if the element is found by the browser.

try:

alert(document.getElementById("fileUploadHidden+" + id));

and see if it is [object] or null.

Then you can check wether it is the document.getElementById or the click method that makes the problem.

Here are some related topics:

Community
  • 1
  • 1
Ephedra
  • 831
  • 10
  • 24
0

Its probably a browser issue.

Try this.

var elem = document.getElementById("fileUploadHidden+" + id);
if (elem.onclick) {
   elem.onclick();
} else if (elem.click) {
   elem.click();
}
Md Nazmoon Noor
  • 3,187
  • 1
  • 24
  • 30
0

In my little experience I solved click/tap problems with a library called zepto.js, basically a light jquery library with support to sreen events http://zeptojs.com/

the problem is that click and tap is not exactly same and system can handle it in different mode. Zepto had a method tap() that maybe was the solution for you. you can see gestual methods on http://zeptojs.com/#modules

svcd
  • 124
  • 6