Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calling functions from an input box button

Status
Not open for further replies.

Toka

Technical User
Sep 5, 2003
3
GB
Hi

I wonder if anyone could help me. I am not very good at Javascript. However I have managed to adapt some code. I have produced 3 different functions.

A user enters a number into an input box and when the user presses the solve button it calls a function.

However I want it to call all 3 function, is this possible?

I tried calling all three within the function and used ";" to seperate them this didn't work.

I tried using if statements but just go erroe message, could anyone help me please

I would be most grateful for any advice available.


Regards

Toka

 
I would have expected onclick="one();two();three();" to work but if not you can chain them together. If that's not suitable, post the code and I'm sure someone here will make the required adjustment :p

<input onclick=&quot;one()&quot;>

<script>
function one(){
...
two();
}

function two(){
...
three()
}

function three(){
...
}
</script>

----------
I'm willing to trade custom scripts for... [see profile]
 
Basically, i need different calculations to happen if certain numbers are entered.

I may have done this the long way round but it was the only way I new how.

Here is the code it might help explain it better.


<HTML><HEAD><TITLE>Quadratic Equation</TITLE>

<SCRIPT language=JavaScript>


//Function to Convert string to number

function tonum(obj)
{
return parseFloat(obj);
}
//This Function is called when the &quot;Solve&quot; button is pressed
//It again calls the &quot;solve_quad&quot; function
function inputtest(form, button)

{

solve_quad(form);
return;

}


//Function - Solving of Quadratic Equation
function solve_quad(form)
{

//Conversion of all Input values to Numbers

var b =&quot;-&quot; + tonum(form.inputbox2.value);



//Calculation of Discriminent

if (b<=-6 && b>=-20)

{

var d=b*b-(4*0.2253*-194.4);
}

if(d<0)
{
var e=Math.sqrt(-d);
var neg=true;
}
else
{
var e=Math.sqrt(d);
var neg=false;
}

//NEED IF STATEMENT AS ABOVE HERE ALSO FOR 0.2253 ETC
//COULD DO AS 2 FUNCTIONS AND CALL ABOVE

//Calculating Real and Img parts
var f=-b/(2*0.2253);
var g=e/(2*0.2253);
if(neg)
{
form.outputbox1.value=f+&quot;+&quot;+g+&quot;i&quot;;

}
else
{
form.outputbox1.value=f+g;

}

return;
}

//Function - Solving of Quadratic Equation2
function solve_quad2(form)
{

//Conversion of all Input values to Numbers

var b =&quot;-&quot; + tonum(form.inputbox2.value);



//Calculation of Discriminent

if (b<=-21 && b>=-52)

{

var d=b*b-(4*0.2193*-134.6);
}

if(d<0)
{
var e=Math.sqrt(-d);
var neg=true;
}
else
{
var e=Math.sqrt(d);
var neg=false;
}

//NEED IF STATEMENT AS ABOVE HERE ALSO FOR 0.2253 ETC
//COULD DO AS 2 FUNCTIONS AND CALL ABOVE

//Calculating Real and Img parts
var f=-b/(2*0.2193);
var g=e/(2*0.2193);
if(neg)
{
form.outputbox1.value=f+&quot;+&quot;+g+&quot;i&quot;;

}
else
{
form.outputbox1.value=f+g;

}

return;
}

//Function - Solving of Quadratic Equation2
function solve_quad3(form)
{

//Conversion of all Input values to Numbers

var b =&quot;-&quot; + tonum(form.inputbox2.value);



//Calculation of Discriminent

if (b <-52)

{

var d=b*b-(4*4.635);
}

if(d<0)
{
var e=Math.sqrt(-d);
var neg=true;
}
else
{
var e=Math.sqrt(d);
var neg=false;
}

//NEED IF STATEMENT AS ABOVE HERE ALSO FOR 0.2253 ETC
//COULD DO AS 2 FUNCTIONS AND CALL ABOVE

//Calculating Real and Img parts
var f=-b/(2/4.635);
var g=e/(2/4.635);
if(neg)
{
form.outputbox1.value=f+&quot;+&quot;+g+&quot;i&quot;;

}
else
{
form.outputbox1.value=f+g;

}

return;
}

function clearform(form)
{
form.inputbox2.value=&quot;&quot;;
}


</SCRIPT>

</HEAD>
<BODY>
<FORM method=post>

<DIV align=center>
<CENTER><b>Converts Centistokes to SUS</b><BR>
</CENTER></DIV>
<BR>
<DIV align=center>
<CENTER>
<P><BR>CST: <INPUT size=30 name=inputbox2><BR></P></CENTER></DIV>
<DIV align=center>
<CENTER>
<P><BR>SUS: <INPUT size=30 name=outputbox1><BR></P></CENTER></DIV>
<DIV align=center>
<CENTER>
<P><INPUT onclick=inputtest(this.form,this) type=button value=Solve name=C2F> <INPUT onclick=clearForm(this.form) type=reset value=Reset name=second></P></CENTER></DIV></FORM></BODY></HTML>

thanks

kyra
 
If you want to call all 3 functions, change your inputtest function from:

Code:
function inputtest(form, button)
{
	solve_quad(form);
	return;
}

to:

Code:
function inputtest(form, button)
{
	solve_quad(form);
	solve_quad2(form);
	solve_quad3(form);
	return;
}

Hope this helps,

Dan

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top