How do you get Length of Text in a Mojo TextField?

749 views Asked by At

How do you get the length of the text inside a Mojo TextField?

I'm trying to set a multiLine TextField with a limit of 150 chars, I tried doing it with a counter, but ran into a issue of not being able to decrement the counter when the text was erased, or adding the right number when pasting text, so my new approach was to get the length of the text each time you press a letter.

I've already tried this: (gets called in the charsAllow attribute of the textField)

if (this.controller.get("mensaje").mojo.getValue().length <= 150) {
    return true;
}

this.controller.get("mensaje").mojo.blur();
return false;

but it doesn't work.... I debugged and the function exits just after the line in bold... it doesn't even returns true or false.

I also tried assigning the length value to a variable or assigning the text to a variable and then get the length, but nothing.

It's the same issue. It returns just after the getValue().

Also, maybe because of this issue, the text scrolls instead of wrapping, but when the textField loses focus it wraps the text.

4

There are 4 answers

0
AudioBubble On BEST ANSWER

My first guess would be that "this" isn't being passed into charsAllow properly. Did you .bind(this) the function you're passing as an argument?

0
river On

If 'mensaje' is the HTML id of your text field, try getting it and using .innerHTML().length. In other words, work with the DOM element using Javascript/Prototype functions instead of the Mojo object.

0
figus On

I found this a little odd... the function mojo.getValue() actually works... but not from inside the function called by "charsAllow"..., and also, the function called by charsAllow can't call any other function, it just breaks out of the function doing nothing... does someone have a way to limit the chars in a MultiLine TextField??? (mojo textfield, to preserve the look :D). Thanks!!

0
r.lewis On

this blog explains a bit about text fields might be useful: http://kmdarshan.com/wordpress/?p=3305