SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast dlh1989's Avatar
    Join Date
    Nov 2004
    Location
    Sunshine Coast, QLD, Australia
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    onBlur field sum

    OK, there is probably a really simple way of solving my problem but I can't think of it. Can anyone please help me? My problem is this: I have three text fields. I want the value of the first one multiplied by 6 and added to the value of the second field. And the total is to be the value of the third field. If that didn't make sense here is my coding:

    Code:
     
    <form name="score" method="POST"> 
     
    <input type="text" name="hg" value="" onBlur="document.score.hs.value=this.value*6">
    <input type="text" name="hb" value="" onBlur="document.score.hs.value=document.score.hs.value+this.value">
     
    <input type="text" name="hs" value="">
     
    </form>
    Therefore if i type 10 in the hg field and 5 in the hb field then hs should be 10x6+5 and should be 65 but instead i get 605. Why is this and is there a way to get my answer?

  2. #2
    SitePoint Addict rokc's Avatar
    Join Date
    Oct 2004
    Posts
    280
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dlh1989
    Therefore if i type 10 in the hg field and 5 in the hb field then hs should be 10x6+5 and should be 65 but instead i get 605. Why is this and is there a way to get my answer?
    Thats because javascript is considering that text box input as string rather that numbers...so it is concatenating it...
    enclose the values of the text box inside parseInt()
    Code:
    <input type="text" name="hg" value="" onBlur="document.score.hs.value=parseInt(this.value)*6">
    <input type="text" name="hb" value="" onBlur="document.score.hs.value=parseInt(document.score.hs.value)+parseInt(this.value)">
     
    <input type="text" name="hs" value="">

  3. #3
    SitePoint Enthusiast dlh1989's Avatar
    Join Date
    Nov 2004
    Location
    Sunshine Coast, QLD, Australia
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Rokc it worked. Thank you very much.


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
  •