Using keyup like @experimentX mentioned is the way you want to go b/c then you'll know that your user has inputed value then. However, running a for loop would be extremely costly on every single keyup event. Instead, since you know the value you want already, you can use a preset regexp to search for your value:
<input type="text" id="text" value="" />
<script>
$(function () {
var $input = $('#text');
$input.keyup(function (e) {
var regexp = /\[text\:/i,
val = $(this).val();
if (regexp.test(val)) {
console.log('i have it: ', val);
}
});
});
</script>
Here are a couple additional scenarios on how you can write the actual regexp.
- You want the string to be at the very beginning of the input:
var regexp = /^\[text\:/i;
- Building on the one above, but incorporate any amount of whitespace in front of the text you actually want:
var regexp = /^\s+?\[text\:/i;