Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-20-2006, 03:54 PM   PM User | #1
guvenck
Regular Coder

 
Join Date: Jan 2006
Posts: 377
Thanks: 8
Thanked 1 Time in 1 Post
guvenck is an unknown quantity at this point
Change text field value upon checkbox clicking

Hi,

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?


How can I do that?

Thanks,
Guvenc
guvenck is offline   Reply With Quote
Old 01-20-2006, 04:03 PM   PM User | #2
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,378
Thanks: 3
Thanked 466 Times in 453 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
Code:
<!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);
 if (obj.checked){
  tar.value=parseFloat(tar.value)+parseFloat(obj.value)
 }
 else {
  tar.value=parseFloat(tar.value)-parseFloat(obj.value)
 }
}
//-->
</script></head>

<body>
<input id="fred" value="100" ><br>
<input type="checkbox" name="" value="20" onclick="Cal(this,'fred');">
<input type="checkbox" name="" value="30" onclick="Cal(this,'fred');">
<input type="checkbox" name="" value="2" onclick="Cal(this,'fred');">
</body>

</html>
vwphillips is offline   Reply With Quote
Old 01-20-2006, 07:40 PM   PM User | #3
guvenck
Regular Coder

 
Join Date: Jan 2006
Posts: 377
Thanks: 8
Thanked 1 Time in 1 Post
guvenck is an unknown quantity at this point
That solves the problem. Many thanks for this quick reply. You are great!
guvenck is offline   Reply With Quote
Old 01-21-2006, 02:39 PM   PM User | #4
guvenck
Regular Coder

 
Join Date: Jan 2006
Posts: 377
Thanks: 8
Thanked 1 Time in 1 Post
guvenck is an unknown quantity at this point
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?



Quote:
function Cal(obj,id){
var tar = document.getElementById(id);
if (obj.checked){
tar.value = parseFloat(tar.value) - parseFloat(obj.value);
if (tar.value < 0) {
alert("No credits left");
tar.value = 0;
obj.checked = false;
return false;
}
}
else {
tar.value = parseFloat(tar.value) + parseFloat(obj.value);
}
}
guvenck is offline   Reply With Quote
Old 01-21-2006, 04:09 PM   PM User | #5
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,378
Thanks: 3
Thanked 466 Times in 453 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
Code:
<!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>
vwphillips is offline   Reply With Quote
Old 01-21-2006, 05:57 PM   PM User | #6
guvenck
Regular Coder

 
Join Date: Jan 2006
Posts: 377
Thanks: 8
Thanked 1 Time in 1 Post
guvenck is an unknown quantity at this point
How many times do I have to thank you?
guvenck is offline   Reply With Quote
Old 01-25-2006, 09:48 PM   PM User | #7
guvenck
Regular Coder

 
Join Date: Jan 2006
Posts: 377
Thanks: 8
Thanked 1 Time in 1 Post
guvenck is an unknown quantity at this point
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;
}

Quote:
<input type="checkbox" name="bs" value="1" onClick="javascript:Cal(this,'fred'); toggleCheckbox2();">
before&nbsp;
<input type="text" class="inputbox" name="bscustom" size="2" maxlength="2" value="<?php echo "$value"; ?>">
days&nbsp;
guvenck is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:00 AM.


Advertisement
Log in to turn off these ads.