Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

contains is object has no method?

I'm trying to find whether there is some character in my string in JS

Like that :

$('.upld_btn').bind("click", function () {

    changeApiFormat($('#embbed').val());

});

if ($('#embbed.contains("?vid=")')) {
....

....
}

I've got an error in my google chrome console :

Object XXXX has no method 'contains'

How come?!

like image 607
thormayer Avatar asked Nov 22 '25 03:11

thormayer


1 Answers

I assume #embbed is an input value of some kind (because of your call to .val() above). So, you would want to pull its value here, as well, and use indexOf (-1 indicates not found):

if ($("#embbed").val().indexOf('?vid=') != -1) {

Here is a jsFiddle illustrating proof-of-concept. Enter "Hi", or "aHia", or anything like that (case-sensitive) into the box and click the link, and you will get a success popup. Otherwise you will get a failure popup.

Code used:

​$(document).ready(function() {
    $(".clicky").click(function() {
        if ($("#input").val().indexOf('Hi') != -1) {
            alert("Contains 'Hi'!");​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
        } else {
            alert("No luck!");
        }
        return false;
    ​}​);
});​
like image 102
Cat Avatar answered Nov 24 '25 16:11

Cat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!