On Aug 27, 3:20 pm, MrNase <[EMAIL PROTECTED]> wrote:
>                         if ($('#myTextarea').val().indexOf('hello') != -1)  {
>                                         log.empty().html('test');
>                                 }
>                                 else if 
> ($('#myTextarea').val().indexOf('moo') != -1)  {
>                                         log.empty().html('test2');
>                                 }
>
> typing in 'moo hello moo' should produce 'test2' but it just does
> nothing. :(

'moo hello moo' should produce 'test', as you do the 'hello' test
first and only do the 'moo' test if 'hello' is NOT found.

Note that your code is searching the DOM several times for the same
element. You can optimize that with:

var text = $('#myTextarea').val();
if( text.indexOf(....) ) { .... }

Whenever you use $(xxx) multiple times for the same xxx, you should
consider saving a reference to the returned result instead of calling $
(xxx) again (which has to repeat all of the work to find the
element(s) again).

Reply via email to