This is a migrated thread and some comments may be shown as answers.

OnChange event returns old value

5 Answers 5268 Views
NumericTextBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Ian
Top achievements
Rank 1
Ian asked on 20 Jul 2010, 09:53 AM
Hi, I've added an OnChange event handler to an IntegerTextBox control. The associated function is fired correctly when the textbox value is changed using the spinner or keyboard, but when I request the value of the textbox, I get the old value prior to the change. Is there a way to get the new value of the textbox? Example code below.

<% Html.Telerik().IntegerTextBox()
    .Name("Risk")
    .MinValue(1)
    .MaxValue(10)
    .IncrementStep(1)
    .Spinners(true)
    .Value(3)
    .ClientEvents(events => { events.OnChange("change"); })
    .Render();
%>

<script type="text/javascript">
    function change() {
        var textBox = $("#Risk").data("tTextBox");
        alert(textBox.value());   
     }
</script>

Regards, Ian

5 Answers, 1 is accepted

Sort by
0
Accepted
Georgi Krustev
Telerik team
answered on 20 Jul 2010, 10:18 AM
Hello Ian Price,

OnChange event is cancellable. Hence the value() will return old value because it is not updated yet.
To get new value I will suggest you to check the following code snippet:
function change(e) {
    $console.log('OnChange :: oldValue: ' + e.oldValue + ', newValue: ' + e.newValue);
}

This is shown in the Client-events example of the numeric text box.

Best wishes,
Georgi Krustev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Ian
Top achievements
Rank 1
answered on 20 Jul 2010, 11:14 AM
Georgi,
Hi. Thanks for the quick reply - your solution works fine. I was working from the .chm documentation file that was included with the controls download and that doesn't seem to mention the oldValue/newValue properties. I'll check the online help next time! :-)

Regards, Ian
0
Derek Hunziker
Top achievements
Rank 1
answered on 03 Sep 2010, 12:04 AM
This is great but it makes it difficult in some cases. I have a client function that, when evoked, loops through all values on the page and counts them up. In my case I wanted all the NumericTextBoxes on the page to evoke this function OnChange. I don't want to have to worry about which textbox evoked the function to be able to get the new value vs. old value.

I suggest breaking it up into two Client Functions: OnChange (cancelable) and OnChanged.

0
Derek Hunziker
Top achievements
Rank 1
answered on 03 Sep 2010, 12:24 AM
Just a quick note for anyone else having similar problems as above...

I found that you can just set the TextBox value to the new value before performing any other functions:

function OnChange(e){
    $(this).data("tTextBox").value(e.newValue);
     
    // Call external function here
    //...
}
0
Derek Hunziker
Top achievements
Rank 1
answered on 03 Sep 2010, 06:55 PM
Turns out the code above doesn't work with CurrencyTextBox. When you set the value, the textbox will add the decimal places which moves the cursor to the end. This makes it impossible to type 234 and have it set the value to $234.00 (It tries to enter $2.0034).

I will have to resort to attaching a jQuery blur event to the textbox for now and grabbing the value directly... There must be a better way...

$('input[name=MyCurrencyTextBox-text]').live('blur', function(){
    //...
});
Tags
NumericTextBox
Asked by
Ian
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Ian
Top achievements
Rank 1
Derek Hunziker
Top achievements
Rank 1
Share this question
or