How to determine the type of input

There seems to be no Modernizr solution here, but how can I detect support for a specific input type, not an attribute?

For example, for an attribute:

var supportsRequired = 'required' in document.createElement('input') if (supportsRequired) { // support } else { // no support } 

Is there an easy way to do this using input types? One thing I don’t want to do is swap input type if it is not supported. Something like this to check only? ...

 var supportsEmailType = 'email' in input.type if (supportsEmailType) { // support } else { // no support } 

Any help was appreciated.

+4
source share
1 answer

From here :

If your browser supports this type of input, the type property will retain the value you set. If your browser does not support this type of input, it will ignore the value you set and the type property will still be "text".

This will correctly determine if the "email" type is supported, and you can reuse this function to detect other types:

 if (supportsInputType('email')) { // support } else { // no support } function supportsInputType(type){ var i = document.createElement('input'); i.setAttribute('type', type); return i.type === type; } 
+2
source

Source: https://habr.com/ru/post/1485502/


All Articles