0

I want to detect fields that are autofilled by chrome in js. The following will throw an exception

_isAutofilled: function () {
    if (this.el.find(':-webkit-autofill').length > 0) {
        return true;
    }
    return false;
},

Error: Syntax error, unrecognized expression: unsupported pseudo: -webkit-autofill

This will throw either:

$('input:-webkit-autofill')

Is there a clean way to prevent jquery from throwing, even though the pseudo selector is not supported in FF?

I don't want to integrate feature detection and I can't use try/catch either.

jQuery Version: 2.2.4

Tom M
  • 2,815
  • 2
  • 20
  • 47
  • Is there a reason why you can't use `try/catch`? That's the easiest way to check if a browser supports a particular pseudo-selector, since non-support results in throwing an error, which will be caught. – Terry Jun 18 '18 at 10:58
  • It belongs to my companies coding standards. Once you wrap something in try/catch, you won't have meaningful errors if something else breaks. – Tom M Jun 18 '18 at 10:59
  • 3
    Those standards don't make sense: I know `try/catch` can be abused, but in this case it is the most straightforward way of checking for pseudo-selector support. It's akin to saying `while` loops can iterate indefinitely so we ban them. – Terry Jun 18 '18 at 11:00
  • that might be true but I can't really start arguing there now. Can you think of another solution? – Tom M Jun 18 '18 at 11:05
  • _“I want to detect fields that are autofilled”_ - what’s the purpose of this? – CBroe Jun 18 '18 at 11:09
  • @CBroe see https://github.com/Baedda/floating-form-labels/issues/12 – Tom M Jun 18 '18 at 11:10
  • 1
    So what you got here is basically another reason to add to the list of https://medium.com/simple-human/floating-labels-are-a-bad-idea-82edb64220f6 ;-) – CBroe Jun 18 '18 at 11:11
  • 1
    @JamesHill the question in the duplicate is about checking general feature support (box-shadow in that case), but here the question is about which specific elements `:-webkit-autofill` actually applies to … So I don’t think that solution from over there can be applied here. – CBroe Jun 18 '18 at 11:14
  • `check.style['webkitAutoFill']` will always return undefined – Tom M Jun 18 '18 at 11:15
  • 1
    @CBroe, you could be right. I didn't know that I could gold-badge reopen also, but I do now. I removed the dupe close. Having said that, feature support of some kind seems to be the only way to solve this... – James Hill Jun 18 '18 at 11:16

0 Answers0