Currently gotem fails when target is input component. I suggest to change this line
range.selectNodeContents(target)
to
range.selectNode(target)
Or even check if node is an input.