Click to See Complete Forum and Search --> : Passing value question


maw_webdesign
05-09-2003, 08:45 AM
Hi there.

I have a dropdown list with 2 options. And I have a textfield Total that already has a value (numeric). I'd like to do the following:

If I choose option 1 from my dropdown list, textfield Total should add 15 to it's total.
If I choose option 2 from my dropdown list, the textfield should add 25 to it's total.

Is this possible?

Thanx...

Leon

Jona
05-09-2003, 09:02 AM
Yes, it is.

<script>
function add(slctVal){
document.myForm.Total.value+=slctVal;
}
</script>

And in your form:

<form name="myForm">
<select name="mySlct" onChange="add(this.options[this.selectedIndex].value)">
<option selected value="15">15</option>
<option value="25">25</option>
</select><br>
<input type=text name="Total" value="5">
</form>

maw_webdesign
05-09-2003, 09:18 AM
Hi there...

Thnx for your reply, but your example doesn't work.

Is something misspelled? Or is it just me?

Thnx again,

Leon

khalidali63
05-09-2003, 09:29 AM
Here is a tiny bit of tweeking of Jonas code
:D

<script type="text/javascript">
function Process(slctVal){
var total = parseInt(document.myForm.Total.value);
document.myForm.Total.value = total + parseInt(slctVal);
}

</script>
</head>

<body class="body">
<form name="myForm">
<select name="mySlct" onChange="Process(this.options[this.selectedIndex].value)">
<option selected value="-1">Select One</option>
<option value="15">15</option>
<option value="25">25</option>
</select><br>
<input type=text name="Total" value="5">
</form>

Jona
05-09-2003, 09:31 AM
<html><head>
<script language="javascript" type="text/javascript">
function x_0(slctVal){
document.myForm.Total.value=parseFloat(document.myForm.Total.value)+parseFloat(slctVal);
}
</script>
</head><body>
<form name="myForm">
<select name="mySlct" onChange="x_0(this.options[this.options.selectedIndex].value)">
<option selected value="15">15</option>
<option value="25">25</option>
</select><br>
<input type=text name="Total" value="5">
</form></body></html>

maw_webdesign
05-09-2003, 09:38 AM
Thnx for the quick replies...

It's working now, but is it also possible to do this with radio buttons?
And then add the amount only once?

Thnx again,

Leon

Jona
05-09-2003, 09:42 AM
<html><head>
<script language="javascript" type="text/javascript">
function x_0(slctVal){
var total = 5; //default value
document.myForm.Total.value=total+parseFloat(slctVal);
}
</script>
</head><body>
<form name="myForm">
<input type="radio" name="elmer" onClick="x_0(this.value)" value="15"><br>
<input type="radio" name="elmer" onClick="x_0(this.value)" value="25"><br>
<input type=text name="Total" value="5">
</form></body></html>

maw_webdesign
05-09-2003, 09:52 AM
Yessss, thats it.

Many thanx, I'm happy now...

:)

Leon