x0x
Aug 22nd 2008, 4:39 pm
Here is my javascript. The function of it is that there are 2 text boxes where users can write numbers. Gun is worth 5000, so they write 2 in gun text box I need the javascript to print out 10000. The code is perfect and I know that it works but how do I print it out? I have never dealed with javascript before, I just found this code. Oh yes, the printed amount must be with commas and can only be a number, never negative.
<script language="JavaScript" type="text/JavaScript">
<!--
function addCommas(nStr)
{
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
function characterCount(x) {
var messageField = document.getElementById(x);
// document.getElementById('charsBox').innerHTML = addCommas(parseInt(document.getElementById('charsBox').innerHTML) + (parseInt(messageField.value) * 2);
var gun = document.getElementById('gun').value;
var car = document.getElementById('car').value;
if(! gun)
gun = 0;
if(! car)
car = 0;
var total = gun * 5000 + car * 2500;
if(total <= x) {
document.getElementById('charsBox').style.color = "#FF9900";
document.getElementById('charsBox').firstChild.nodeValue = addCommas(total);
} else {
document.getElementById('charsBox').style.color = "#990000";
document.getElementById('charsBox').firstChild.nodeValue = addCommas(total);
}
}
//-->
</script>
How do I print the result?
Any help is greatly appreciated.
<script language="JavaScript" type="text/JavaScript">
<!--
function addCommas(nStr)
{
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
function characterCount(x) {
var messageField = document.getElementById(x);
// document.getElementById('charsBox').innerHTML = addCommas(parseInt(document.getElementById('charsBox').innerHTML) + (parseInt(messageField.value) * 2);
var gun = document.getElementById('gun').value;
var car = document.getElementById('car').value;
if(! gun)
gun = 0;
if(! car)
car = 0;
var total = gun * 5000 + car * 2500;
if(total <= x) {
document.getElementById('charsBox').style.color = "#FF9900";
document.getElementById('charsBox').firstChild.nodeValue = addCommas(total);
} else {
document.getElementById('charsBox').style.color = "#990000";
document.getElementById('charsBox').firstChild.nodeValue = addCommas(total);
}
}
//-->
</script>
How do I print the result?
Any help is greatly appreciated.