0

I want to check if user is using iPhone/iPad/iPod and if true, hide element.

How to do it using javascript?

Tried to do it like this: Hide HTML element if user is on an iOS device?

But it didnt work, i put the code in my controller in AngularJS

Code I tried:

$(document).ready(function(){  
  var iOS = false,
  p = navigator.platform;
  if( p === 'iPad' || p === 'iPhone' || p === 'iPod' ) {
     iOS = true;
  }
  if (iOS === false) {
     $("input[type=button]").hide();
  }
});
Bruno Peres
  • 15,845
  • 5
  • 53
  • 89
Nemanja G
  • 1,760
  • 6
  • 29
  • 48

1 Answers1

1

I want to check if user is using iPhone/iPad/iPod and if true, hide element.

But in the code shown you are testing if iOS variable is false! You should change the test to if (iOS === true).

Tip: Also, you can do some improvements in your code and make it more elegant. For example:

$(document).ready(function(){  

    var appleDevices = ['iPad', 'iPhone', 'iPod'];

    if (appleDevices.indexOf(navigator.platform) > -1) {
        $("input[type=button]").hide();
    }

});

This code does the same thing as the one posted by you, but using an array and indexOf. That is: hide input[type=button] when the value of navigator.platform is an element of the appleDevices array.

Bruno Peres
  • 15,845
  • 5
  • 53
  • 89