Reputation: 155
I want to calculate inside the grid,multiple of 2 textBox values, key-press or keyup event. can u provide me some javascript for this. I use the following code it does not work for me javascript
var a = document.getElementById(txtlabchrgsNooflab);
var b = document.getElementById(txtlabchrgsTotalCost);
txtlabchrgsTotalCost=a*b;
Upvotes: 0
Views: 202
Reputation: 103797
Firstly - if you want to multiply two things together, they're going to have to be numbers.
At the moment your a
and b
variables are simply the HTML elements you've identified, and you can't multiply those together. (Though pay attention to Carl's comment; you may need to quote txtlabchrgsNooflab
if this is meant to be a literal string and not a variable name.)
So you need to get the value of these elements, as a numeric datatype. Getting the value should be as simple as calling .value()
for text inputs; however, this returns a string. This may get automatically coerced to a numeric type as required, but I would be happier (and think it's clearer) to call parseFloat
on it definitively.
Once you have your two floats, you can merrily multiply them. So your code should look something like the following:
var elemA = document.getElementById(txtlabchrgsNooflab);
var elemB = document.getElementById(txtlabchrgsTotalCost);
var a = parseFloat(elemA.value());
var b = parseFloat(elemB.value());
txtlabchrgsTotalCost = a * b;
OK, so using this above code results in:
error: Unable to get value of the property 'value': object is null or undefined
What can we infer from this error message? Well, that the runtime wasn't able to compute value
because the thing it was being called on was null or undefined.
If you look at the code, it's clear which variable(s) might be null or undefined - and you should be able to inspect your HTML to see why this is, and make the required corrections.
Upvotes: 2