PDA

View Full Version : Javascript doesnt want to work..


Shaolins-Finest
10-19-2007, 03:43 PM
Ah, fixed :D

But I have another question, how can I add digits in realtime ? Like if a product costs £5 and the user inputs 2 into a textbox it will automatically workout the total. Would javascript suffice, or php ?

coothead
10-19-2007, 05:02 PM
Hi there Shaolins-Finest,

have a look at this example, it may suit your requirements...
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<style type="text/css">
#container {
width:180px;
border:1px solid #000;
padding:10px;
font-family:sans-serif;
font-size:1em;
background-color:#ccc;
margin:auto;
}
#container:after {
content:'';
display:block;
clear:both;
}
#container label {
width:100px;
float:left;
text-align:right;
margin-bottom:10px;
}
#container input {
width:60px;
float:left;
margin-left:10px;
}
</style>

<script type="text/javascript">
var df;
window.onload=function() {
df=document.forms[0];
df[1].onkeyup=function() {
if(isNaN(this.value)) {
alert('Note:- please enter a whole number, not text');
df.reset();
return;
}
if(this.value!=Math.floor(this.value)){
this.value=Math.floor(this.value);
alert('Note:- this value has been converted to a whole number');
}
calculateCost(this.value);
}
}
function calculateCost(qty){
prc=parseFloat(df[0].value.replace('£',''));
df[2].value='£'+prc*qty;
}
</script>

</head>
<body>

<form action="#">
<div id="container">

<label>price :</label>
<input type="text" readonly="readonly" value="£5.00">

<label>quantity :</label>
<input type="text">

<label>cost :</label>
<input type="text" readonly="readonly" value="£0.00">

</div>
</form>

</body>
</html>