Unable to retrieve textbox value and subtract from it
Ive a textbox with id f开发者_StackOverflow社区nlprce I want to retrieve its value on an event, subtract 50 from it and update it to the latest value, ive this piece of code to work over:
function updateDiscount() {
var iniprce = parseInt($("#fnlprce").text());
var fnlprce = iniprce-50;
$("#dscntdprce").html("Price: " + fnlprce + ");
$("fnlprce").html(fnlprce);
}
But something getting wrong as it is showing, ‘NaN’ in the output, guidance please :)
Change
var iniprce = parseInt($("#fnlprce").text());
to
var iniprce = parseInt($("#fnlprce").val());
because val() is current value of that textbox.
Besides that, change
$("#dscntdprce").html("Price: " + fnlprce + ");
to
$("#dscntdprce").html("Price: " + fnlprce);
because yours one have syntax error
Use .val()
instead of .text()
as an input's value is found in its value=
attribute, and not as a text node inside of it.
function updateDiscount() {
var iniprce = parseInt($("#fnlprce").val(), 10);
var fnlprce = iniprce - 50;
$("#dscntdprce").html("Price: " + fnlprce);
$("#fnlprce").html(fnlprce);
}
精彩评论