javascript - (JQuery) Use of word or phrase in textbox triggers action -
i have working jquery looks phrase , trigger:
$("#id_message").on('keyup', function(){ if($('#id_message').val().tolowercase().indexof('foo foo') > -1){ doegg(); } });
however love able expand list of words or phrases. have separate list this, assume need in array?
var phraselist = [ "foo foo", "foobar", "fooby foo foobar" ];
how can search word list? imagine list having maybe 5 phrases or so. , if user types "i foo foo" or "i love @ foobar" should still trigger textbox contains words list.
also added problem, have script undoes function if user decides delete phrase:
$("#id_message").on('keyup', function(){ if($('#id_message').val().tolowercase().indexof('foo foo') == -1){ undoegg(); } });
edit #1
the closest answer have found comes @nixful-autistic:
$("#id_message").on('keyup', function(){ for(var i=0;i<phraselist.length;i++){ if($('#id_message').val().tolowercase().indexof(phraselist[i]) > -1){ doegg(); } } });
edit #2
i believe know problem is!
the value of .indexof textbox set -1 works way through characters til finds word or phrase phraselist array...and assigns value > -1.
so whenever there keyup event, value of indexof set -1 until can find 1 of words of phrases. causes doegg() , undoegg() functions called , uncalled in quick succession repeatedly.
so think need way scan through whole textbox before assigning .indexof value.
is possible?
$("#id_message").on('keyup', function(){ for(var i=0;i<phraselist.length;i++){ if($('#id_message').val().tolowercase().indexof(phraselist[i]) > -1){ doegg(); } } });
update
i wish suggestion
$("#id_message").on('keyup', function(){ for(var i=0;i<phraselist.length;i++){ if($('#id_message').val().tolowercase().indexof(phraselist[i]) > -1){ doegg(); }else{ undoegg(); } } });
update 2 try declare these variables outside function
var change; var changeback;
function doegg(){ if(changeback)clearinterval(changeback); //if changeback isrunning audioelement.play(); //var change = setinterval(function () { change = setinterval(function () { ... } function undoegg(){ if(change)clearinterval(change); //if change isrunning audioelement.pause(); //var changeback = setinterval(function () { changeback = setinterval(function () { ... }
Comments
Post a Comment