Results 1 to 4 of 4

Thread: This is a valid javascript function ?

  1. #1
    Join Date
    Oct 2004
    Posts
    425
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default This is a valid javascript function ?

    This is a valid javascript function ? ac=checkbox & days,adults=textboxes &
    euro=result textbox...
    Code:
    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 ?

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,625
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Place the below mentioned code just after your HTML form

    Code:
    <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.

  3. #3
    Join Date
    Oct 2004
    Posts
    425
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default

    MAY USE AND

    Code:
    <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 ?

  4. #4
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,625
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    You code is following:
    Code:
    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.
    Code:
    	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

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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •