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
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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.