0

I am trying to use the typeWatch plugin for jQuery. I have this javascript:

 <script type="text/javascript">
 $(document).ready(function() {
   var options = {
        callback: function() { alert("a-ok!  " + $(this).text); },
        wait: 333,
        highlight: true,
        captureLength: 1
    }

    $("#customer").typeWatch(options);
 });
 </script>

And my view's HTML has this:

 Method B: <%= Html.TextBox("customer") %>

With this test, if I type "ABC" in the textbox, I am expecting an alert to popup with "a-ok! ABC". Instead the following shows up...

a-ok!  function (F) {
    if (typeof F !== "object" && F != null) {
        return this.empty().append((this[0] && this[0].ownerDocument || 
               document).createTextNode(F));
    }
    var E = "";
    o.each(F || this, function () {o.each(this.childNodes, function () {
          if (this.nodeType != 8) {
             E += this.nodeType != 1 ? this.nodeValue : o.fn.text([this]);}});});
   return E;
}

I've tried changing the $(this).text to .val, and I've also tried referencing the input field more directly as $("#customer") but they yield similar results. How can I access just the entered text?

1
  • The reason you're getting back a text representation of the function is because you're not invoking the function using parens... text should be text***()*** Commented Jun 3, 2009 at 13:27

2 Answers 2

5

You should use the val() function on the textbox:

$(this).val();
Sign up to request clarification or add additional context in comments.

2 Comments

I suspect this is generally the correct answer, but I couldn't get it to work for some reason. Since Lucas solved my problem he gets the answer, but +1 to this as well since its probably the correct answer 99% of the time.
You needed the Lucas the typeWatch specialist for this job ;). Hope you remember my -broader- tip: $(elem).val() not .text ! (works for every html inputfield) hehe
1

Looking the code of typeWatch version 2.0 I found this:

    function checkElement(timer, override) {
        var elTxt = jQuery(timer.el).val(); <----------- !!! this...

        // Fire if text > options.captureLength AND text != saved txt OR if override AND text > options.captureLength
        if ((elTxt.length > options.captureLength && elTxt.toUpperCase() != timer.text) 
        || (override && elTxt.length > options.captureLength)) {
            timer.text = elTxt.toUpperCase();
            timer.cb(elTxt); <-------------- !!! ...goes here.
        }
    };

timer.cb is the callback that you set up in the options. So you just add a parameter in that callback function that you use and that parameter will be the text from the input.

1 Comment

sweet! thanks! i kept trying different variations of the other answer since 4 people upvoted it quickly but nothing was working. this worked like a charm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.