The very idea of the attributes is to help users and make the page more accessible, so it would be against the very idea to refer to nonexistent elements with them. If the attribute is used at all, it will be used to refer to elements in a manner that matters to the user, so of course there can be trouble – even if the assistive software or the browser handles the error the best possible way.
It is also formally incorrect, because the attribute has been declared as containing id
attribute values, and a string cannot be such a value when the id
attribute does not exist.
So, you should simply make your script modify the attribute value to contain an id
attribute value as soon as you have added that value, but not sooner.