0

javascript function which is used in OnChange event

function calc(A,B,SUM) { 
    var one = Number(A); 
    if (isNaN(one)) { alert('Invalid entry: '+A); one=0; } 
    var two = Number(document.getElementById(B).value);  
    if (isNaN(two)) { alert('Invalid entry: '+B); two=0; } 
    document.getElementById(SUM).value = parseInt(one) + parseInt(two);
} 

I have forloop for create 10 input fox

Input boxes

<?php
for($k=1;$k<=$textboxindex;$k++)
{
    echo "<input type=\"text\" class='textboxsizeqty' name=\"qty[]\" value=\"$qty\" id         ='op1' onChange=\"calc('this.value','op2','result');\"/>";
    echo "<br/>";
}
?>

Input boxes

<?php
for($k=1;$k<=$textboxindex;$k++)
{
    echo "<input type=\"text\" class='textboxsize' name=\"rate[]\" value=\"$rate\" id      ='op2' onChange=\"calc('this.value','op2','result');\" />";
    echo "<br/>";
}
?>


<?php
for($k=1;$k<=$textboxindex;$k++)
{
    echo "<input type=\"text\" class='textboxsize' name=\"sum[]\" value=\"\"     id='result'/>";
    echo "<br/>";
}
?>

It's doesn't work here what I am doing. I'm New in PHP. Thanks

2
  • 2
    try it without this.value in parenthesis so remove these: ' Commented Oct 26, 2012 at 20:30
  • what happens if you try this ? Commented Oct 26, 2012 at 20:30

1 Answer 1

0

Please try and why do you give the quotes in this.value.

<?php
for($k=1;$k<=$textboxindex;$k++)
{
echo "<input type=\"text\" class='textboxsizeqty' name=\"qty[]\" value=\"$qty\" id         ='op1' onChange=\"calc(this.value,'op2','result');\"/>";
echo "<br/>";
}
?>
Sign up to request clarification or add additional context in comments.

3 Comments

and the sun is not correct like example 5 + 5 = 55 >i dont know how to deal with it. Can you help plz.
document.getElementById(SUM).value = parseInt(one) + parseInt(two); --change javascript final line like this .

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.