views:

194

answers:

2

I have a bunch of NumberTextBoxes which I created programmatically. Now, I have to compute some values on keypress so I call a function for that... Looks something like this:

...
<head>
...
function setNumTextBox() {
    ...
    var val = {
        name: "mytextbox",
        onKeyPress: keyPressFxn
    }
    new dijit.form.NumberTextBox(val, "mytextbox");
}

function keyPressFxn(evt) {
    // do the processing here
}
...

Now, my problem is this. The user types, say, "12". When the user types in "1", dijit.byId("mytextbox").attr("value") is "". When the user types in the "2", the value is now "1". In short, the value (I've also tried the "displayedValue" attribute) that I get in the function is not the latest.

I tried to use "intermediateChanges: true" but it doesn't work for the text box for this case. Now, I went ahead and used the "onChange" event instead of "onKeyPress". I get the correct values but... I don't know what called the onChange event.

Questions:

  1. If I am to use the "onKeyPress" event, how do I get the latest value? Meaning, if I type in "123", I'll get "123" when I access the field's value/displayedValue.

  2. If I use "onChange", how do I get the id of the element that fired the onChange event? (it's not as simple as evt.target.id right? Tried that one and it didn't work)

  3. Or, do I have to use a combination of the 2? Like, get the id of the caller via onKeyPress then get the updated value that the user typed via onChange?

Thanks!

A: 

This should get you close to what you wanted. You may need to track the dojo.keys.CLEAR and dojo.keys.DELETE constants as well.

function keyPressFxn(evt) {
  var value = this.value;
  if (dojo.keys.BACKSPACE == evt.keyCode ) {
    console.log(value.substr(0, value.length - 1));
  } else {
   console.log(value + String.fromCharCode(evt.charCode));
  }
}
jgchristopher
ah, that may be it... thanks!
callie16
A: 

I would seriously consider using the onkeyup event instead. found this in another stackoverflow answer:

Because you are using key-press event. key press has 3 phase:

  1. Key down: when key is press
  2. Key hold: key is hold down
  3. Key up: key is release In your case, problem can be solved by using keyup event
Will Olbrys
thanks, will look into that :)
callie16