I’m converting prices using jquery and a php script that pulls in the latest exchange rate from Yahoo!
My update function is working on elements that I can update with .text() (eg span elements) but as soon as I try to set the .val() of my input element, it simple receives ‘NaN’ – even though I’m using the same value for both.
function frmtCurrency(ele,price) {
// round the currency to the nearest .05
price *= 2.0;
price = price.toFixed(1) / 2.0;
price = price.toFixed(2);
//alert (parseInt(price)) here returns the amount, so it IS a number
// check what type of element 'ele' is
var element = (ele.get(0).tagName);
if (element != 'INPUT') {
ele.text(price+code); ALWAYS OK eg 10.55
} else {
ele.val(price); // ALWAYS NaN
}
};
Can anyone tell me why I can’t update an input field but I can update a span element
Odds are you’re passing a string or something wrong at some point in your software, giving you a NaN. Here you can see I pass a empty string and I add a handle to make it appear as a 0.00 when no real number is given.
http://jsfiddle.net/kuroir/DaHSA/1/