unixmiah unixmiah - 4 months ago 12
Javascript Question

JQuery insert calculated value into a form text field

I have a form below

<form id="credit-card-submission-form-1" class="visual-form-builder vfb-form-1 " method="post" enctype="multipart/form-data">


and I have this following field within the
<form
tag.

<input
type="text" name="vfb-17" id="vfb-17" value="" class="vfb-text vfb-medium required " />


I want to take input value from this field below, lets say if
400
was entered from
id="vfb-15
field, I want to take the
400
and apply
3.25%
to this field
id="vfb-17"
.

<input
type="text" name="vfb-15" id="vfb-15" value="" class="vfb-text vfb-medium required " />


This is what I have tried so far just to test out if I can access the field:

<script type="text/javascript">
jQuery(document).ready(function($) {
$('#vfb-17').val( $('#vfb-17').val() + 'My new text' )
});
</script>


I can't get this value/'My new text' even inserted into the field in the above JQuery.

Answer

try this :

$('#vfb-17').keyup(function(){
  var vfbVal = $(this).val();
  var vfb15Val = (vfbVal*3.5)/100;
  $('#vfb-15').val(vfb15Val+'My new text');
});

jsfiddle