Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Javascript String Prob

Javascript String Prob

Scheduled Pinned Locked Moved Web Development
javascripthelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tadhg88
    wrote on last edited by
    #1

    hi i am just tryin to get a total for 4 textboxes on a webpage but when i use the following code to add all the vales together they are concatenated like a string and not numbers any help wud be appreciated Thanks in advance Tim window.onload = initAll; function initAll() { document.getElementById("technicalScore1"). onblur = GetTechincalTotal; document.getElementById("technicalScore2"). onblur = GetTechincalTotal; document.getElementById("technicalScore3"). onblur = GetTechincalTotal; document.getElementById("technicalScore4"). onblur = GetTechincalTotal; } function GetTechincalTotal() { var total = 2; total += document.getElementById("technicalScore1"). value; total += document.getElementById("technicalScore2"). value; total += document.getElementById("technicalScore3"). value; total += document.getElementById("technicalScore4"). value; document.getElementById("technicalScoreTotal"). innerText = total; }

    S C 2 Replies Last reply
    0
    • T tadhg88

      hi i am just tryin to get a total for 4 textboxes on a webpage but when i use the following code to add all the vales together they are concatenated like a string and not numbers any help wud be appreciated Thanks in advance Tim window.onload = initAll; function initAll() { document.getElementById("technicalScore1"). onblur = GetTechincalTotal; document.getElementById("technicalScore2"). onblur = GetTechincalTotal; document.getElementById("technicalScore3"). onblur = GetTechincalTotal; document.getElementById("technicalScore4"). onblur = GetTechincalTotal; } function GetTechincalTotal() { var total = 2; total += document.getElementById("technicalScore1"). value; total += document.getElementById("technicalScore2"). value; total += document.getElementById("technicalScore3"). value; total += document.getElementById("technicalScore4"). value; document.getElementById("technicalScoreTotal"). innerText = total; }

      S Offline
      S Offline
      Sreekumar P P
      wrote on last edited by
      #2

      by default value picks as string. you will have to pasre it to integer using parseInt. parseInt("document.getElementById('technicalScore1'). value",10) Sreekumar PP

      1 Reply Last reply
      0
      • T tadhg88

        hi i am just tryin to get a total for 4 textboxes on a webpage but when i use the following code to add all the vales together they are concatenated like a string and not numbers any help wud be appreciated Thanks in advance Tim window.onload = initAll; function initAll() { document.getElementById("technicalScore1"). onblur = GetTechincalTotal; document.getElementById("technicalScore2"). onblur = GetTechincalTotal; document.getElementById("technicalScore3"). onblur = GetTechincalTotal; document.getElementById("technicalScore4"). onblur = GetTechincalTotal; } function GetTechincalTotal() { var total = 2; total += document.getElementById("technicalScore1"). value; total += document.getElementById("technicalScore2"). value; total += document.getElementById("technicalScore3"). value; total += document.getElementById("technicalScore4"). value; document.getElementById("technicalScoreTotal"). innerText = total; }

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        try this: total += (document.getElementById("technicalScore1").value * 1); The multiplication will force it to be an int and not a string. I assume you stop the user from entering non numbers ?

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups