0

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'))

3

3 Answers 3

7
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())) {
Sign up to request clarification or add additional context in comments.

Comments

0

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/

Comments

0

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.

2 Comments

I don't think :contains considers the value of input fields. Only the text content of elements.
@FelixKling you're right, updating answer to reflect the idea and why it won't work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.