Click to See Complete Forum and Search --> : Checkbox help
NaZKYN
08-29-2006, 11:50 PM
I'm a complete n00b when it comes to checkboxes :o
Anyway what I wanna do is when a checkbox is checked, the script adds a number to a variable, changing the output, this is what I have (not working atall) -
JavaScript
var tokensOn = eval(document.calc[5].value)
if (tokensOn == true) {
usedPockets = usedPockets + 2000
}
<input type=checkbox onclick="if (this.checked) document.calc[5].value="true">
Pleeeeeease help lol, thanks in advance :)
What is calc[5]. A name? and indexed name? Whose name? A text field in a form?
Anyway the reference and the syntax are incorrect.
NaZKYN
08-30-2006, 01:30 AM
Calc is the form name and [5] is the 5th input, it's working for [0]., [1]., [2]., [3]. and [4]. :)
<!--Begin
function answer() {
var emptyPockets = eval(document.calc[0].value)
var usedPockets = eval(document.calc[1].value)
var labSpace = eval(document.calc[2].value)
var junkies = eval(document.calc[3].value)
var result = 0
while (usedPockets > junkies - labSpace * 17) {
result = result + 1
usedPockets = usedPockets - junkies + labSpace * 17
junkies = junkies + 1
}
var visits = eval(document.calc[4].value)
if (visits == 0) {
result = result - 2
usedPockets = usedPockets + 1
}
var tokensOn = eval(document.calc[5].value)
if (tokensOn == true) {
usedPockets = usedPockets + 2000
}
alert("You can spend " + result + " tokens on junkies" + "," + " You need to purchase " + (emptyPockets - usedPockets) + " drugs" + ".")
}
// End-->
<head>
<script type="text/javascript" src="Junkie Calculator.js">
</script>
<title>DopeWars Junkie Calculator</title>
</head>
<body>
<center>
<form name="calc">
<b>Coat Size:</b> <input type=text>
</input>
<b>Used Pockets:</b> <input type=text>
</input>
<b>Lab Space:</b> <input type=text>
</input>
<b>Total Junkies:</b> <input type=text>
</input><p>
<b>Dealer Visits Remaining:</b> <input type=text>
</input>
<b>Tokens On Credits??</b> <input type=checkbox onclick="if (this.checked) document.calc[5].value="true">
<br>
</br>
<input type="button" value="Submit" onclick="answer()">
</input>
</form>
</center>
</body>
</html>
Like this?:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function answer(){
var emptyPockets = Number(document.calc[0].value);
var usedPockets = Number(document.calc[1].value);
var labSpace = Number(document.calc[2].value);
var junkies = Number(document.calc[3].value) ;
var visits = Number(document.calc[4].value);
var result = 0;
while (usedPockets > junkies - labSpace * 17) {
result++;
usedPockets = usedPockets - junkies + labSpace * 17
junkies++;
}//I really don't understand what this while loop supposes to do
if (visits == 0) {
result -=2;
usedPockets++;
}
if (document.calc[5].checked) {
usedPockets += 2000
}
alert("You can spend " + result + " tokens on junkies" + "," + " You need to purchase " + (emptyPockets - usedPockets) + " drugs" + ".")
}
</script>
</head>
<body>
<form name="calc">
<b>Coat Size:</b> <input type="text">
<b>Used Pockets:</b> <input type="text">
<b>Lab Space:</b> <input type="text">
<b>Total Junkies:</b> <input type="text">
<br>
<br>
<b>Dealer Visits Remaining:</b> <input type="text">
<b>Tokens On Credits??</b> <input type="checkbox">
<br>
<input type="button" value="Calculate" onclick="answer()">
</input>
</form>
</body>
</html>
Don't use eval() there are almost always other more simple and quick methods to evaluate an expression.
NaZKYN
08-30-2006, 10:26 AM
Thanks alot :D
NaZKYN
08-30-2006, 11:47 AM
Sorry mate, I thought that was what I wanted but it wasn't.
What i'm trying to do is say if the checkbox is checked, then don't add to the variable 'junkies' (in the while loop), i've tried this with no luck lol :o
var result = 0
while (usedPockets > junkies - labSpace * 17) {
result = result + 1
usedPockets = usedPockets - junkies + labSpace * 17
junkies = junkies + 1 //<~~this is the line that I wanna change using the if statement (if possible)
if (document.calc[5].checked)
junkies - junkies
}
as I said, I don't understand what your while loops stands for and, generally, I don't understand the general aim of your calculation. Can you provide us some algebrical linear algoritm for all these?