I have a text field with a number value in it. Let's call it CreditsLeft.
Below, I have checkboxes, the visitor checks these boxes and selects some services.
1- When a checkbox is checked, I want to decrease (or increase when unchecked) the CreditsLeft value by one.
2- What if I want a service decrease by one and another service by 2 or another 3?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<script language="JavaScript" type="text/javascript">
<!--
function Cal(obj,id){
var tar=document.getElementById(id);
var val=0
if (obj.checked){
val=parseFloat(tar.value)+parseFloat(obj.value);
}
else {
val=parseFloat(tar.value)-parseFloat(obj.value);
}
if (val<0){
alert('insufficent funds');
obj.checked=false;
return;
}
tar.value=val
}
//-->
</script></head>
<body>
Want to add some control over this:
If the credits is below zero, I want to alert the visitor.
Following code works as I want, but it displays -1 before sending the alert. How can I prevent the -1 from being displayed?
<input id="fred" value="100" ><br>
<input type="checkbox" name="" value="20" onclick="Cal(this,'fred');">
<input type="checkbox" name="" value="-130" onclick="Cal(this,'fred');">
<input type="checkbox" name="" value="2" onclick="Cal(this,'fred');">
</body>
</html>
I have a textbox that is enabled upon clicking a checkbox.
If I use the method above, the credits are calculated even if the visitor does not type anything into the text field. I need the credits to be decreased by 1 if the visitor types anything into the text field. What should I do?
Quote:
function toggleCheckbox2 () {
if (document.AddBirthday.bs.checked) {
document.AddBirthday.bscustom.disabled = false;
} else {
document.AddBirthday.bscustom.disabled = true;
}
}
function Cal(obj,id){
var tar = document.getElementById(id);
var val = 0;
if (obj.checked){
val = parseFloat(tar.value) - parseFloat(obj.value);
} else {
val = parseFloat(tar.value) + parseFloat(obj.value);
}
if (val < 0){
alert('No credits left.');
obj.checked=false;
return;
}
tar.value = val;
}