PDA

View Full Version : This is a valid javascript function ?



leonidassavvides
06-11-2008, 03:43 AM
This is a valid javascript function ? ac=checkbox & days,adults=textboxes &
euro=result textbox...

function calc() {
if (document.book.ac.checked == true) {
ac = 6;
} else {
ac = 0;
}
// document.book.euro.value = 99;
document.book.euro.value = document.book.days.value * (floor((document.book.adults.value)/3) * (47+ac) + 41 + ac) ;
}

How to trigger this function onblur or onchange of days or adults or ac ?

codeexploiter
06-11-2008, 06:03 AM
Place the below mentioned code just after your HTML form



<script type="text/javascript">
document.book.days.onblur = calc;
document.book.ac.onblur = calc;
document.book.adults.onblur = calc;
</script>


Plz note that the above mentioned code is based on the facts you've provided. If anything changes the above code might result in the desired result I fear. Let me know how it works.

leonidassavvides
06-11-2008, 06:27 AM
MAY USE AND


<input name="days" type="text" id="days" value="1" size="1" maxlength="1" onchange="calc();" />
PLEASE NOTE THAT SEEMS THE EXPRESSION
document.book.euro.value = document.book.days.value * (floor((document.book.adults.value)/3) * (47+ac) + 41 + ac) ;
can not calculated, is any error ?
has any importance the sequence of textbox fields in the form , for this ?

codeexploiter
06-11-2008, 06:44 AM
You code is following:

document.book.euro.value = document.book.days.value * (floor((document.book.adults.value)/3) * (47+ac) + 41 + ac) ;

Issues

1. Any value that comes from any form field is string by nature especially in your case you are using the value of days form field. Before using the value in a mathematical expression you need to convert the value into number using parseInt or parseFloat methods.

2. You can't call floor function the way you used. It should be like the following. The floor function comes under Math object.


Math.floor()


3. You have used a variable named 'ac' make sure that it contains number based value in it.

Assuming that you have proper value in 'ac' variable check the following CODE



document.book.euro.value = parseFloat(document.book.days.value) * Math.floor(document.book.adults.value / 3) * (47 + ac) + 41 + ac;