Page 1 of 1

OnChange Event when Edit Forms

Posted: Fri Mar 28, 2014 12:08 pm
by alinapia

How to create onchange event when user edit on text box, ex: textbox A + textbox B = textbox C. So the value on textbox C is automatically filled when A or B changed. Thank's


Re: OnChange Event when Edit Forms

Posted: Fri Mar 28, 2014 12:39 pm
by mobhar

Simply put the following code under: "Client Scripts" -> "Table-Specific" -> "Edit Page" -> "Startup Script":

$("#x_A").on('change keyup paste mouseup', function() {
$("#x_C").val($("#x_A").val() + $("#x_B").val());
});

$("#x_B").on('change keyup paste mouseup', function() {
$("#x_C").val($("#x_A").val() + $("#x_B").val());
});


Re: OnChange Event when Edit Forms

Posted: Sat Mar 29, 2014 10:39 am
by alinapia

Thank you Mobhar, I've tried your code, but nothing happen on textbox C, sorry I'm newbie in web programming. Am i missed something? Do I have to put equal sign after val C?


Re: OnChange Event when Edit Forms

Posted: Sun Mar 30, 2014 7:56 pm
by mobhar

If the name of your fields are "A", "B", and "C" respectively, then PHPMaker will generate the ID of the textbox become: "x_A", "x_B", and "x_C".

So, make sure you have inspected the ID of textboxes, and they are the same with that above.


Re: OnChange Event when Edit Forms

Posted: Mon Mar 31, 2014 11:25 am
by alinapia

It's working now, I missed the check mark on Edit Page, previously i put it on the inline edit. Now it's working but the result is not exactly I want, I want the value on textbox C is sum of the A and B. Ex: A=2, B=3 so the value of C=5, but now the value of C is 23. Please how to fix this?


Re: OnChange Event when Edit Forms

Posted: Tue Apr 01, 2014 9:25 am
by danielc

Use javascript parseInt() function to convert the string to integer before your add:
var a = $("#x_A").val();
a = parseInt(a);
var b = $("#x_B").val();
b = parseInt(b);

Then,
$("#x_C").val(a+ b);


Re: OnChange Event when Edit Forms

Posted: Tue Apr 01, 2014 3:12 pm
by alinapia

it's working as expected now, after change the code from Mobhar :

$("#x_A").on('change keyup paste mouseup', function() {
$("#x_C").val(+$("#x_A").val() + +$("#x_B").val());
});

$("#x_B").on('change keyup paste mouseup', function() {
$("#x_C").val(+$("#x_A").val() + +$("#x_B").val());
});

Thank you.


Re: OnChange Event when Edit Forms

Posted: Mon May 05, 2014 9:17 am
by siriok

if field A is a float tye and field B is a integer.
can it calculate due to this code??
i' tried but it don't work


Re: OnChange Event when Edit Forms

Posted: Mon May 05, 2014 11:22 am
by danielc

The code will work. Post your modified code for discussion.


Re: OnChange Event when Edit Forms

Posted: Tue May 06, 2014 11:15 am
by siriok

my field "sell" is a auto fill value (float type)
and field "amount" is for user to type in (integer type)
field "sumcost" is a result: (float type)
so my code is

$("#x_amount").on('change keyup paste mouseup', function() {
$("#x_sumcost").val(+$("#x_sell").val() * +$("#x_amount").val());
});

i put in in client script -> table-specific ->add/copy paste -->startup script
so do in the edit page too.
but when i run it don't matter. what wrong with it??


Re: OnChange Event when Edit Forms

Posted: Tue May 06, 2014 2:04 pm
by mobhar

Are you sure the "sell" field has already had the correct value? If you change the multiply (*) sign with plus (+) sign, doesn't it work, too?