Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery contains doesn't work on Chrome

I have one problem with jquery contains. It work perfect on firefox.

This is my code.

$("input[data-height='cm']").blur(function(){
    var text = $(this).val();

    if($(this).val().length > 0) {
        if(!$(this).val().contains("cm")) {
            $(this).val(text + " cm");
        }
    }
});

on chrome it give error

Uncaught TypeError: $(...).val(...).contains is not a function

How can I fix it please help.

Thank you.

like image 945
Aram Mkrtchyan Avatar asked May 12 '15 12:05

Aram Mkrtchyan


1 Answers

There is no such method as contains in Chrome. Instead, you should better use indexOf:

// Instead of...
if ($(this).val().contains('cm')) { /* val() contains 'cm' */ }
// ...do this:
if ($(this).val().indexOf != -1) { /* val() contains 'cm' */ }

Please note that indexOf returns -1 if occurence is not found in string and >= 0 if occurence is found.

like image 62
Andrew Dunai Avatar answered Sep 22 '22 05:09

Andrew Dunai