I think this is wrong regardless of the rest, because value can definitely be a string, and valueToIndex(value) first tries to use the value to compare against element's attributes, which can be strings.