How to add HTML5 attribute "autocomplete" to form fields? According to an accessibility test, I have to add the HTML5 attribute "autocomplete" an the proper tags to all form fields on my website where the user fills in his/ her personal data. 

E.g. "Your E-Mail" > autocomplete="email", "Your Family Name" > autocomplete="family-name", etc. (The proper tags are listet in the WHATWG HTML standard https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#autofill)

Some of the fields are in Drupal core (like the user registration form), others are webforms (like an event registration). 

What is the best way to achieve this in Drupal 8?
I think it's possible to add this attribute to all input fields using a bit of JS, something like this should work:

(function($, Drupal) {
  Drupal.behaviors.autocompleteFieldBehavior = {
    attach: function (context) {
      $(context).find('input').each(function () {
        var name = $(this).attr('name');
        if ($(this).attr('autocomplete').length === 0) {
          if (name) {
            $(this).attr('autocomplete', name);
          }
          else {
            $(this).attr('autocomplete', 'Default autocomplete value.');
          }
        }
      });
    }
  };
})(jQuery, Drupal);


The other solution would be alter all forms using hook_form_alter, see @berramou's comment.