Skip to main content
Participating Frequently
July 20, 2006
Question

Onclick display result in textbox

  • July 20, 2006
  • 3 replies
  • 1554 views
Could anybody please help me with simple multiply code in coldfusion. I am trying to multiply price * quantity and get result total onclick Textfield. Below is the code I am using, but is not working:

<SCRIPT language="JavaScript">
function checkValue(thisTxt, tbox1Id, tbox2Id)
<!--
{
var txt1Obj = document.getElementById(tbox1Id);
var txt2Obj = document.getElementById(tbox2Id);
thisTxt.value = txt1Obj.value * txt2Obj.value;
}

//-->
</SCRIPT>

<cfparam name="txt1Obj" default="">
<cfparam name="txt2Obj" default="">
<cfoutput>
<form action="../action/return.html" method="get"
enctype="application/x-www-form-urlencoded">
<p align="center"><strong><span class="style2">Price: </span>
<input type="text" name="txt1Obj" value=""> </strong> </p>
<p align="center"><strong><span class="style2">Quantity:</span>
<input type="text" name="txt2Obj" value=""> </strong> </p>
<p align="center"><strong><span class="style4">Total:</span>
<input onclick="checkValue" value="" name="thisTxt"
type="text"></input>
</form>
</cfoutput>

Thanks
This topic has been closed for replies.

3 replies

Participating Frequently
July 24, 2006
It seems to me that you have a few things confused. First of all, what you are trying to attempt with the above code is multiplying using javascript; your coldfusion code could be taken out completely and it wouldn't effect the core of what you are trying to do.

In your function, you are being given three elements (thisTxt, tbox1id,tbox2id). Since you are being given these elements, there is no reason to use the getElementById function. It seems you are trying to merge two different approaches into one. The first approach is to be passed the elements in the function; the second approach is to get the elements from the page using getElementById. Here is the code that does what you want using javascript, demonstrating both ways:
Inspiring
July 20, 2006
In terms of simply getting it to work, you have to convert the numeric strings to numbers using either parseInt() or parseFloat() before doing math. Once you get that working, you'll want to userproof it by ensureing the values are numbers.

Also, you are not using getElementById() properly.
Inspiring
July 20, 2006
The first mistake I see is that you need some parentheses () in your
onClick event.

<input onclick="checkValue()" value="" name="thisTxt"

You may also want to modify the total line in the JavaScript function.

document.getElementByID('thisTxt').value = txt1Obj.value * txt2Obj.value;

See if that helps.

Just to clarify for any future readers of this thread, this is purely a
JavaScript issue and it is irrelevant that this is a ColdFusion template
it could be HTML, ASP, or any other web application technology.

forumnotifier wrote:
> Could anybody please help me with simple multiply code in coldfusion. I am
> trying to multiply price * quantity and get result total onclick Textfield.
> Below is the code I am using, but is not working:
>
> <SCRIPT language="JavaScript">
> function checkValue(thisTxt, tbox1Id, tbox2Id)
> <!--
> {
> var txt1Obj = document.getElementById(tbox1Id);
> var txt2Obj = document.getElementById(tbox2Id);
> thisTxt.value = txt1Obj.value * txt2Obj.value;
> }
>
> //-->
> </SCRIPT>
>
> <cfparam name="txt1Obj" default="">
> <cfparam name="txt2Obj" default="">
> <cfoutput>
> <form action="../action/return.html" method="get"
> enctype="application/x-www-form-urlencoded">
> <p align="center"> <span class="style2">Price: </span>
> <input type="text" name="txt1Obj" value="">
</p>
> <p align="center"> <span class="style2">Quantity:</span>
> <input type="text" name="txt2Obj" value="">
</p>
> <p align="center"> <span class="style4">Total:</span>
> <input onclick="checkValue" value="" name="thisTxt"
> type="text"></input>
> </form>
> </cfoutput>
>
> Thanks
>