Web Hosting Talk







View Full Version : help with some simple javascript on select code


housefire
02-07-2008, 11:51 PM
So ive got a drop down select menu with options 1 - 10. (i.e. "1" "2" "3" etc..)

When the user selects 3 from the select box, i need to do a math equation of 3 x 10 and put the result of 30 into a text field.

Anyone got a function that might help?

Kohrar
02-08-2008, 12:28 AM
Try:


<head>
<title>Javascript Demo</title>
</head>

<body>
<script>
function updateValue() {
document.getElementById('Return').innerHTML = document.getElementById('selectField').value * 10;
document.getElementById('ReturnText').value = document.getElementById('selectField').value * 10;
}
</script>
<form>
<select name="test" id="selectField" onChange="updateValue();">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>

<br />
<!-- Text inside <span> -->
<span id="Return"></span>

<br />

<!-- Or, just form input box -->
<input type="text" id="ReturnText" value="" />

</form>
</body>
</html>



This will just update the value on both the textbox and on the page. If you need any explaination about the html/javascript, don't hesitate to ask :)

housefire
02-08-2008, 01:42 AM
that worked perfect, however now im trying to update a 3rd text box with the sum of the first one.

the code now is

document.getElementById('subtotal').value = (document.getElementById('attendee_total').value + document.getElementById('total_dinner').value);

but instead of adding 100 + 50 as 150, its outputting 10050 as the result.

Ideas?

Kohrar
02-08-2008, 01:57 AM
Looks like it's concatenating the two values, maybe because it's reading it as a string... I guess you can try parseInt(), or parseFloat() for decimals.


document.getElementById('subtotal').value = parseInt(document.getElementById('attendee_total').value) + parseInt(document.getElementById('total_dinner').value);

joeshacks
02-08-2008, 01:59 AM
that worked perfect, however now im trying to update a 3rd text box with the sum of the first one.

the code now is

document.getElementById('subtotal').value = (document.getElementById('attendee_total').value + document.getElementById('total_dinner').value);

but instead of adding 100 + 50 as 150, its outputting 10050 as the result.

Ideas?

thats because javascript thinks the values are strings, you need to convert them to integers or floats..

try something like..

document.getElementById('subtotal').value = eval((document.getElementById('attendee_total').value) + eval(document.getElementById('total_dinner').value));

housefire
02-08-2008, 02:33 AM
thats because javascript thinks the values are strings, you need to convert them to integers or floats..

try something like..

document.getElementById('subtotal').value = eval((document.getElementById('attendee_total').value) + eval(document.getElementById('total_dinner').value));


try this but it didnt work.

housefire
02-08-2008, 02:35 AM
Looks like it's concatenating the two values, maybe because it's reading it as a string... I guess you can try parseInt(), or parseFloat() for decimals.


document.getElementById('subtotal').value = parseInt(document.getElementById('attendee_total').value) + parseInt(document.getElementById('total_dinner').value);




works perfect.


thanks.

Kohrar
02-08-2008, 02:37 AM
Glad it worked! Good luck with your project :)