Javascript String Prob
-
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; }
-
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; }
by default value picks as string. you will have to pasre it to integer using parseInt. parseInt("document.getElementById('technicalScore1'). value",10) Sreekumar PP
-
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; }
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 )