jquery - Recognize when writing in between two $$ signs -
i have textfield in i'm writing text.
for sake of simplicity, want alert('boom!')
jumps out every time start writing in between 2 $$.
for example have blank textfield , start typing (cursor "|" sign)
today nice day|
nothing happens, start typing
today nice day, $|$
still nothing, when start typing
today nice day, $sometext|$
alert box should jump out each letter in between dollar signs.
why need kind of feature? want live-equation-preview (mathjax rendering) every time user starts typing his/her equation, , can recognize it's equation $$ signs (everything in between rendered).
edit: multiple $$'s possible in textfield. script must recognize 1 active (cursor position between it's $$'s).
you can use jquery caret plugin
http://examplet.buss.hk/download/download.php?plugin=caret.1.01
note: edit , delete these characters Ôªø line 8
and replace
funcition. ( not replace use index of matched pattern )
$("#mytext").bind("keyup", function(e){ var text = $(this).val(); var caret = $(this).caret().start; if(text && text.length > 0){ text.replace(/\$.*?\$/g, function(m, n){ if(caret > n && caret < (n + m.length)){ alert("boom"); } }); } });
demo : http://jsfiddle.net/xqxxb/
just showing how it. can improve this.
use
$("#mytext").bind("keyup keydown change", function(e){ ...
for better result.
Comments
Post a Comment