[SalesForce] Can i add two number field dynamically on Visual force page

I have two input field of type number Field1 and field2 on Opportunity page .
Here is my requirement
If i enter field1= 5 then field2 should auto populate 30+5=35 on the same screen.

I am struggling in it since 2 days.

Please help me out.
Thanks in advance!

Best Answer

Add an onchange attribute to the apex:inputField (Field1) that calls a JavaScript function when Field1 is changed to evaluate the value you want for Field2, e.g.:

function updateField2(field1val) { 
  document.getElementById(yourField2ID).value = field1val.value + 30;
}

<apex:inputField onchange="updateField2(this);" id="field1">

Take care in accessing VF elements from JavaScript, as it can be a little frustrating based on how Visualforce generates the HTML - you can either use $Component or use the generated element tree syntax.