Debt Consolidation - Find jobs - Bob's Free Stuff Forum - Credit Reports - Medios de comunicación

PDA

View Full Version : A very simple thing for someone who has ever done anything with java.


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.

Dondon2d
Aug 22nd 2008, 5:14 pm
It depends, if you are planning to just 'echo' it, you can use document.write, if you're planning to put in a text field then you can use document.formname.fieldname.value = total;

x0x
Aug 22nd 2008, 5:17 pm
No not text field. It's like a card system. Users type the amount of stuff they want and it calculates the stuff together and prints it out.
What is exactly that I have to put there? With the brackets and all. I'm a serious noob lol

I tried this <script>document.write.value = total;</script> and no result. Also if there is nothing it should display plain zero. From the original code that I adopted I found this <label id="charsBox"><0</label> but I doubt that does anything. In fact, it didn't.

Dondon2d
Aug 22nd 2008, 5:24 pm
Well your code needs to be rewritten to fulfill your needs.

x0x
Aug 22nd 2008, 5:27 pm
How come? I took it from another site and it worked fine there, just the print out part isn't showing up in the source for some reason, or I'm doing something wrong.
gun car are the field names by the way.

x0x
Aug 22nd 2008, 5:34 pm
It's <label id="charsBox"><0</label> for sure, I tested it. For some reason it doesnt work on my own page...

Ok it's probably something to do with where I put the javascript. Where should I put it to make it work? right now I pasted it inside some html stuff

Dondon2d
Aug 22nd 2008, 5:37 pm
Show me the page where you got the code.

jack_ss
Aug 22nd 2008, 6:33 pm
Your code works perfectly fine. You just need to run your function.

First, I'd probably put your functions in the <head></head> or an included JS file.

Second, and here's your problem, your code needs to be run by something. Probably wherever the "x" value is set (notice characterCount(x), "x" needs to be an argument). Maybe your need something like this:

<input name="x" type="text" value="0" onkeyup="characterCount(this.value);">

To make this function work you will also need 2 input fields with "gun" and "car" ids.

x0x
Aug 23rd 2008, 5:42 am
Thank you, I think I can take it from here, although I have one more question. How do I put a php variable in here

<input name="x" type="text" value="0" onkeyup="characterCount(this.value);">

"this.value" must be a php variable


this didn't work for some reason: onKeyUp="characterCount(<? echo ($variable[0]); ?>);">

Thanks again.


edit: it was my bad, the variable was called in the bottom of the page