indexOf() bug in Chrome

Oct 16, 2013 14:53


I've one page with few tabs(made using jquery ui)
So I've one field (type=text) on the hidden tab and second one text field on active tab.
If first field is empty and user starts typing something in second field then script checks if first field is empty or taking a part of second field and if this case is true will auto-populate first field. The mechanism of checking made through indexOf.
So following logic appears:
second_field_val.indexOf(first_field_val). All works well while we are typing text. But when white space comes to first field indexOf returns -1.
So second_field_val.indexOf(first_field_val) returns -1 which is incorrect. After testing I've noticed that this is appearing only in Chrome.
If I'll get values of variables and try to check this line in JS console then 'test 1'.indexOf('test ') will work fine. I don't know the reason for this but simple hack coming quickly: second_field_val.indexOf($.trim(first_field_val)).
So we are simply removing white spaces in the beginning and in the ending of the line using jquery's $.trim function.

jquery js chrome

Previous post Next post
Up