6 Replies - 459 Views - Last Post: 15 March 2011 - 12:33 PM

#1 ankur.una  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 15-March 11

Code Not Giving Output

Posted 15 March 2011 - 11:59 AM

I am making simple Javascript calculator and problem with this piece of code is that its not giving output after clicking any button.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Javascript Calculator</title>
<script language="javascript" type="text/javascript">
  function multiply(){
a=Number(document.calculator.number1.value);
b=Number(document.calculator.number2.value);
c=a*b;
document.calculator.total.value=c;
}
function divide(){
a=Number(document.calculator.number1.value);
b=Number(document.calculator.number2.value);
c=a/b;
document.calculator.total.value=c;
}
function add(){
a=Number(document.calculator.number1.value);
b=Number(document.calculator.number2.value);
c=a+b;
document.calculator.total.value=c;
}
function subtract(){
a=Number(document.calculator.number1.value);
b=Number(document.calculator.number2.value);
c=a-b;
document.calculator.total.value=c;
}
</script>
</head>
<style type="text/css">
#first {
	  margin-left:68px;
	  width:50px;
}
#second{
	margin-left:50px;
	width: 50px;

}
#multiply{
	
	
	border-color:blue;
	color:blue;
	background-color:#6CF;
}
#divide{
	
	
	border-color:blue;
	color:blue;
	background-color:#6CF;
}
#add{
	
	
	border-color:blue;
	color:blue;
	background-color:#6CF;
}
#subtract{
	
	
	border-color:blue;
	color:blue;
	background-color:#6CF;
}
#ans{
	
	
	margin-left:140px;
	  width:50px;
}


</style>

<body onload="alert('Welcome to the Calculator!')">
<h1>Intro To Javascript Calculator Assignment</h1>

<table cellpadding="20px" width="310" border="1" >
<td width="274">
<form name="calculator">
Enter First Number: <input type="text" name="number1" id="first">
<br /> 
<br />
Enter Second Number: <input type="text" name="number2" id="second"> 
<br />
<br />

<input type="button" id="multiply"  value="MULTIPLY" onclick="javascript:multiply();" >
<input type="button" id="divide" value="DIVIDE" onclick="javascript:divide();">  
<input type="button" id="add" value="ADD" onclick="javascript:add();">  
<input type="button" id="subtract" value="SUBTRACT" onclick="javascript:subtract();">  
<br />

Answer: <input type="text" id="ans" name="total"> 
</form>
</td>
</table>
</body>

</html>




Is This A Good Question/Topic? 0
  • +

Replies To: Code Not Giving Output

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3530
  • View blog
  • Posts: 10,172
  • Joined: 08-June 10

Re: Code Not Giving Output

Posted 15 March 2011 - 12:14 PM

are there any messages in the error console?

PS. you donít need to put javascript: in an event attribute, those only have javascript code inside it.
Was This Post Helpful? 0
  • +
  • -

#3 ankur.una  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 15-March 11

Re: Code Not Giving Output

Posted 15 March 2011 - 12:17 PM

View PostDormilich, on 15 March 2011 - 12:14 PM, said:

are there any messages in the error console?

PS. you donít need to put javascript: in an event attribute, those only have javascript code inside it.

When I run it on any browser, it does not give any output at all. I would be grateful if you could point me any possible error.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: Code Not Giving Output

Posted 15 March 2011 - 12:20 PM

I think you have too many elements named multiply/add/subtract/divide. Try giving your functions names that differ from other elements. Like doMultiply().
Was This Post Helpful? 0
  • +
  • -

#5 ankur.una  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 15-March 11

Re: Code Not Giving Output

Posted 15 March 2011 - 12:25 PM

@JackOfAllTrades Oh, just figured it out. Thank you very much for kind help. :stupid:
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3530
  • View blog
  • Posts: 10,172
  • Joined: 08-June 10

Re: Code Not Giving Output

Posted 15 March 2011 - 12:26 PM

that could pose a problem in IE, which automatically assigns the names and ids to variables.

as for the error console, check here.
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: Code Not Giving Output

Posted 15 March 2011 - 12:33 PM

It exhibited the same behavior for me on FF3.6.15 on Mac until I changed the function name(s). Firebug told me there was no function named multiply.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1