Fixing an javascript error which was caused by getting the selected text