vbd
vbd

Reputation: 3557

jQuery(selector).contains throws "no method" TypeError?

my HTML:

<form>
    <input type="text" name="url" maxlength="10" />
</form>

my JS:

$(document).ready(function(){
    var inputUrl =  $('input[name=url]');
    inputUrl.change(function() {
        alert(inputUrl);
        if ($(inputUrl).contains('watch'))
        {
            alert ('Contains watch: yes');
        }
        else
        {
            alert ('Contains watch: no');
        }
    });
});

Chrome Console shows:

Uncaught TypeError: Object [object Object] has no method 'contains'

What's the correct syntax for:

if ($(inputUrl).contains('watch'))

Upvotes: 0

Views: 1514

Answers (3)

gts
gts

Reputation: 627

My first idea was to have input[name=url] changed over to input[name=url]:contains('watch') or make inputUrl var parse so in the code, but this will only fetch text from within an element and won't have effect with regards to the value of an attr. @user1689607's answer looks like the best one in retrospect.

Upvotes: 0

I Hate Lazy
I Hate Lazy

Reputation: 48779

if (inputUrl.val().indexOf('watch') > -1) {

Or perhaps you only want to operate on the current one that received the event.

if ($(this).val().indexOf('watch') > -1) {

Or make it case insensitive:

if (/watch/i.test($(this).val())) {

Upvotes: 7

YogeshWaran
YogeshWaran

Reputation: 2281

You can do like this . conatins psuedo is used to search text in the innerHtml. it does not search in input element value

$(inputUrl).val().indexOf('watch') > 0

reference : http://api.jquery.com/contains-selector/

Upvotes: 0

Related Questions