Search code examples
javascriptjquerykeyup

If textarea contains certain string then


I am creating a contact form for my website, and I am adding a few joke easter eggs which trigger if a user chooses a certain option or in this case happens to type a certain string into the message textarea.
I would like to be able to trigger the action as they type...

I have tried to use indexOf(), but that didn't work or maybe I was using it wrong.

$("#id_message").keyup(function(){
    if($('#id_message').val().indexOf('foo') > -1){
        // do something
    }
});

What is the correct way of doing this?

Thanks


Solution

  • Try this:

    $("#id_message").on('keyup', function(){
        if($('#id_message').val().indexOf('foo') > -1){
            // do something
        }
    });