0

I have a HTML form with a field

    <input type='text' name="HR" id="HR" onblur="totalSum(this);"/>

I am trying to get this value in the javascript file as follows

     function totalSum(obj)
      {

         var HR=document.getElementById("HR").value;
      }

suppose I am trying to enter 150 in the HR field, then it is not able to fetch it in the HR variable in the javascript file. Could you please guide me where am I going wrong.

as suggested by @crayon I am trying to do something like this .. thanks for your reply. I am trying to do something like this........

    if(parseInt(HR)>=100 && parseInt(HR)<150)
      {
      sum1=0;
       }
    else if(parseInt(HR)>= 150 || parseInt(HR)<= 100) 
    {
      sum1=1;
    }
   else if(parseInt(HR)>= 180 || parseInt(HR)<= 90)  
    {
    sum1=2;
    }
   else if(parseInt(HR)>= 190 || parseInt(HR)<= 80) 
    {
     sum1=4;
     }

 and i changed onkeypress to onblur, but still its not working ...

Code for using sum1.

    document.getElementById('Total').value =  parseInt(sum1)+parseInt(sum3)+parseInt(sum5)+parseInt(sum6)+parseInt(sum7);
2
  • 1
    type="VARCHAR(45)" Um, what? Commented Nov 18, 2013 at 3:35
  • because you used onkeypress event. Commented Nov 18, 2013 at 3:36

4 Answers 4

1

1) type should be text

2) you need to change it to something like onblur or else add a separate button to assign an onclick event to

3) make sure to wrap HR in Integer() to convert it to a number

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for your reply.. I have updated the question with some more information as per suggestion, but still its not working. Could you please have a look whether what I am doing is right.
@user1495220 okay your edit doesn't show that you've changed the type='varchar(45)' to type='text' there's no such thing as the latter in html. 2nd, when you changed it to onblur, did you change your focus after entering in the number? e.g. did you click somewhere else on the page so that the input field would lose focus? Because that is what onblur means
@user1495220 reason why onkeypress doesn't work is because it invokes the function after every char you enter in, instead of waiting until you are done entering in the full value. that's why i said to change it to onblur or else make a separate button to attach click event to
@user1495220 also, what are you actually doing with sum1 ?
Yes I clicked somewhere else after entering the value.There is a field called total in which I have to display sum as per the value entered or selected for various variable. I am updating the code for that also for your reference.
0

It because you had used onkeypress event. You code will work but the previous values will display on second entry.. Try with onKeyUp.

Comments

0

Here is a simple example (demo)

<input type='text' id='input' onKeyUp='readInput(this)'/>
<br/>
<input type='text' id='output' readonly='readonly'/>

With the following script:

function readInput(input) {
    var value = input.value;
    document.getElementById('output').value = value;
};

Comments

0

Use onkeyup in place of onkeypress

<input type="text" maxlength="45" name="HR" id="HR" onkeyup="totalSum(this);"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.