Make sure getElementById always returns the first element with the given ID in tree order.(fixes #1822)

see #1822