Jump to content

Need help with Javascript


eva21

Recommended Posts

I have a form, and there are 3 textboxes. Two of the textboxes the user enters a number, and the third one automatically multiplies them together. I wana make it so as soon as there is two valid numbers in these two textboxes, i want it added asap. Im not sure what to use onclick, onblur so any help is appreciative.

 

<input type=text readonly size="3" MAXLENGTH="3" onclick="doCalc()" id="tot1" name="txt1" value="">

 

<input type=text readonly size="3" MAXLENGTH="3" onclick="doCalc()" id="tot1" name="txt2" value="">

 

<input type=text readonly size="3" MAXLENGTH="3" onclick="doCalc()" id="tot1" name="total" value="">

 

<script>

function doCalc()

{

var txt1 = document.getElementById('txt1').value;

var txt2 = document.getElementById('txt2').value;

}

</script>

 

I know there isnt much for javascript, but i keep getting an error for the second line! Please help

Link to comment
https://forums.phpfreaks.com/topic/133534-need-help-with-javascript/
Share on other sites

<input type=text size="3" MAXLENGTH="3" onkeyup="doCalc()" id="txt1" name="txt1" value="">

<input type=text size="3" MAXLENGTH="3" onkeyup="doCalc()" id="txt2" name="txt2" value="">

<input type=text readonly size="3" MAXLENGTH="3" id="total" name="total" value="">   

<script>
function doCalc()
{
   document.getElementById('total').value = '';
   var txt1 = parseInt(document.getElementById('txt1').value);
   var txt2 = parseInt(document.getElementById('txt2').value);
   if(txt1 && txt2){
     document.getElementById('total').value = txt1 * txt2;
   }
}
</script>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.