Reputation: 791
I have one input field where numeric value will be entered and on another read only field the converted (calculated) value will be displayed dynamically.
I tried with jQuery but it is not working.
Html
<form name="curconvert" method="get" action="script/conversion.php">
<div class="field-row conversion">
<div class="field1"><input type="text" name="strbasecurrency" id="strbasecurrency" tabindex="1" value="Start typing..." onfocus="if (this.value == 'Start typing...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Start typing...';}" /></div>
<div class="field2">=</div>
<div class="field3"><input type="text" name="strconvcurrency" id="strconvcurrency" value="Result" readonly class="converted"></div>
</div>
</form>
jQuery
$(document).ready(function() {
$("#strbasecurrency").keydown(function (e) {
$('.field-row input').css({'color':'#162d3a'});
// Allow: backspace, delete, tab, escape, enter and .
if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
// Allow: Ctrl+A
(e.keyCode == 65 && e.ctrlKey === true) ||
// Allow: home, end, left, right
(e.keyCode >= 35 && e.keyCode <= 39)) {
// let it happen, don't do anything
return;
}
// Ensure that it is a number and stop the keypress
if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
e.preventDefault();
}
$converted == ($("#strbasecurrency").val() * 0.73);
$("#strconvcurrency").val("$converted");
});
});
Upvotes: 0
Views: 299
Reputation: 63956
You had several issues:
==
operator to assign a value when you should have used =
""
The updated code should look like this:
$(document).ready(function() {
$("#strbasecurrency").keydown(function (e) {
$('.field-row input').css({'color':'#162d3a'});
// Allow: backspace, delete, tab, escape, enter and .
if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
// Allow: Ctrl+A
(e.keyCode == 65 && e.ctrlKey === true) ||
// Allow: home, end, left, right
(e.keyCode >= 35 && e.keyCode <= 39)) {
// let it happen, don't do anything
return;
}
// Ensure that it is a number and stop the keypress
if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
e.preventDefault();
}
//added "var" and used = instead of ==
var $converted = ($("#strbasecurrency").val() * 0.73);
$("#strconvcurrency").val($converted); //removed double quotes around $converted
});
});
Upvotes: 2
Reputation: 337550
The issue is with the last two lines of your code where the syntax is a little off. Try this:
var converted = $("#strbasecurrency").val() * 0.73;
$("#strconvcurrency").val(converted);
Also you need to use the keypress
or keyup
event on your strbasecurrency
field.
Upvotes: 1