What is wrong with this code?its not working

M

Mclaren Fan

<Script type="text/javascript">
function put0(form) {
if (form.out2.type=="text") {
var val=form.out2.value;
var newval=val+0;
form.out2.value=newval;
}
else {
var val=form.out.value;
var newval=val+0;
form.out.value=newval;
}
}
function put1(form) {
if (form.out2.type=="text") {
var val=form.out2.value;
var newval=val+1;
form.out2.value=newval;
}
else {
var val=form.out.value;
var newval=val+1;
form.out.value=newval;
}
}
function put2(form) {
if (form.out2.type=="text") {
var val=form.out2.value;
var newval=val+2;
form.out2.value=newval;
}
else {
var val=form.out.value;
var newval=val+2;
form.out.value=newval;
}
}
function put3(form) {
if (form.out2.type=="text") {
var val=form.out2.value;
var newval=val+3;
form.out2.value=newval;
}
else {
var val=form.out.value;
var newval=val+3;
form.out.value=newval;
}
}
function put4(form) {
if (form.out2.type=="text") {
var val=form.out2.value;
var newval=val+4;
form.out2.value=newval;
}
else {
var val=form.out.value;
var newval=val+4;
form.out.value=newval;
}
}
function put5(form) {
if (form.out2.type=="text") {
var val=form.out2.value;
var newval=val+5;
form.out2.value=newval;
}
else {
var val=form.out.value;
var newval=val+5;
form.out.value=newval;
}
}
function put6(form) {
if (form.out2.type=="text") {
var val=form.out2.value;
var newval=val+6;
form.out2.value=newval;
}
else {
var val=form.out.value;
var newval=val+6;
form.out.value=newval;
}
}
function put7(form) {
if (form.out2.type=="text") {
var val=form.out2.value;
var newval=val+7;
form.out2.value=newval;
}
else {
var val=form.out.value;
var newval=val+7;
form.out.value=newval;
}
}
function put8(form) {
if (form.out2.type=="text") {
var val=form.out2.value;
var newval=val+8;
form.out2.value=newval;
}
else {
var val=form.out.value;
var newval=val+8;
form.out.value=newval;
}
}
function put9(form) {
if (form.out2.type=="text") {
var val=form.out2.value;
var newval=val+9;
form.out2.value=newval;
}
else {
var val=form.out.value;
var newval=val+9;
form.out.value=newval;
}
}
function makevis(form) {
form.out2.type="text";
form.add.type="hidden";
form.equalsadd.type="button";
}
function add(form) {
var firstnum=form.out.value;
var secnum=form.out2.value;
var addoutput=firstnum+secnum;
form.addoutput.value=addoutput;
}
</script>
<form>
<input type="button" name="0" value="0" onClick="put0(this.form)">
<input type="button" name="1" value="1" onClick="put1(this.form)">
<input type="button" name="2" value="2" onClick="put2(this.form)">
<input type="button" name="3" value="3" onClick="put3(this.form)">
<input type="button" name="4" value="4" onClick="put4(this.form)">
<input type="button" name="5" value="5" onClick="put5(this.form)">
<input type="button" name="6" value="6" onClick="put6(this.form)">
<input type="button" name="7" value="7" onClick="put7(this.form)">
<input type="button" name="8" value="8" onClick="put8(this.form)">
<input type="button" name="9" value="9" onClick="put9(this.form)">
<input type="text" name="out" value="">
<input type="hidden" name="out2" value="">
<input type="button" name="add" value="+"
onClick="makevis(this.form)">
<input type="hidden" name="equalsadd" value="="
onClick="add(this.form)">
<br/>
<input type="text" name="addoutput" value="">

</form>
 
A

Andreas Bergmaier

We don't know what you want to do, so the only description of the
expected functionality is the code itself: and it works!
Bergi

PS: you seem to add a number to a string, which automatically casts the
number to a string.
 
L

Lasse Reichstein Nielsen

Mclaren Fan said:
<Script type="text/javascript">

You should probably cut down on these functions. You only
need one:
function put(form, digit) {
var elem = form.elements['out2'];
if (elem.type != "text") elem = form.elements['out1'];
elem.value += digit;
}
}
function makevis(form) {
form.out2.type="text";
form.add.type="hidden";
form.equalsadd.type="button";
}
function add(form) {
var firstnum=form.out.value;
var secnum=form.out2.value;
var addoutput=firstnum+secnum;

As others have pointed out, you need to convert firstnum and secnum to
numbers. They are strings when read from the input element.
form.addoutput.value=addoutput;
}
</script>
<form>
<input type="button" name="0" value="0" onClick="put0(this.form)">
onclick="put(this.form, '0');"
....
<input type="button" name="add" value="+"
onClick="makevis(this.form)">
<input type="hidden" name="equalsadd" value="="
onClick="add(this.form)">

This call top "add" doesn't see the global add function. Instead the "add"
variable resolves to the "add" button just above. The intrinsic event handlers
(javascript written directly in the onclick attribute) is executed in a scope
that contains, amongst other elements, the current form element (like if
you had written "with(form) { ...my handler ...}").

In the debugger in Chrome (start, e.g., by pressing Ctrl-Shift-J)), I
got the error: "Uncaught TypeError: object is not a function" at the
line containing 'onClick="add(this.form)">'. That should be a hint,
since only one function call is performed there. Then you can add an
"alert(add)" to see what object "add" resolves to, and you'll see that
it's an HTML input element.

You got lucky this time, but in the fututre, if you need someone to
help you debug a problem, you need to tell us:

- What you did (the code is good, but also say which keys or buttons
to press to reproduce the problem, if it's not immediately obvious).
Otherwise we'll have to guess how to reproduce it.
- What actually happens. It might not reproduce the same way in other
browsers, and someone who doesn't see the problem, but doesn't know
so, is wasting his time.
- What you expects to happen. Otherwise we'll have to guess what you
want in order to find a fix.

Anything that requires people helping you to guess (with the chance of
guessing wrong) or to ask you for more information, or otherwise spend
time not directly related to finding and fixing the problem, increases
the chance that they won't bother. I.e., in order to get the most help,
it's best to decrease the effort needed to help.

/L 'Tell us what we need to reproduce, recognize and repair the problem'
 
J

John G Harris

<Script type="text/javascript">
function put0(form) {
if (form.out2.type=="text") {
var val=form.out2.value;
var newval=val+0;
form.out2.value=newval;
}
else {
var val=form.out.value;
var newval=val+0;
form.out.value=newval;
}
}
<snip>

You should make some attempt to make your code easier to read. It'll
help you as well.

John


function put0(form)
{
if (form.out2.type=="text")
{
var val=form.out2.value;
var newval=val+0;
form.out2.value=newval;
}
else
{
var val=form.out.value;
var newval=val+0;
form.out.value=newval;
}
} // put0
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
8924D9443D28E23ED5CD>, Sun, 23 Oct 2011 11:25:24, John G Harris
You should make some attempt to make your code easier to read. It'll
help you as well.

And that indentation can be obtained by pasting the script into a copy
of my js-quick.htm and pressing the 'Indt' button. (It alters only the
number of spaces directly following each newline, and unmatched bracket-
like things within strings or comment on a line need to be matched in
trailing comment.)
 
D

Denis McMahon

too much code

I think Lasse has nailed your problem. You have use the same identifier
("add") for an element name and a function name, and that's confusing the
browser.

Rgds

Denis McMahon
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
adnews.com>, Tue, 25 Oct 2011 01:55:00, Denis McMahon
I think Lasse has nailed your problem. You have use the same identifier
("add") for an element name and a function name, and that's confusing the
browser.

The code is horribly repetitive. There seem to be several near-
identical functions, such as

function put3(form) {
if (form.out2.type=="text") {
var val=form.out2.value;
var newval=val+3;
form.out2.value=newval;
}
else {
var val=form.out.value;
var newval=val+3;
form.out.value=newval;
}
}

The "3" should be an argument, saving several functions :

function putN(form, N) {
if (form.out2.type=="text") {
var val=form.out2.value;
var newval=val+N;
form.out2.value=newval;
}
else {
var val=form.out.value;
var newval=val+N;
form.out.value=newval;
}
}

and it could be (beware mis-readings and mis-writings)

function putN(form, N) {
var S = form.out2.type=="text" ? "out2" : "out"
form.value += N
}

Having bow looked at the form : the button names may not be needed,
and if the "put" calls have an argument of just "this" the "put" routine
can both read the button value and locate the form. All untested.
 
D

Denis McMahon

Having bow looked at the form : the button names may not be needed, and
if the "put" calls have an argument of just "this" the "put" routine can
both read the button value and locate the form. All untested.

Something like:

http://www.sined.co.uk/tmp/addition.htm

Got rid of the form, used ids instead of names, and switch between
display:none and display:inline to hide / reveal items.

Rgds

Denis McMahon
 
M

Mclaren Fan

Thanks everyone for all the help i just want to tell you all that you
all are professional people who know javascript thoroughly while i
have just started with javascript 2 days ago and am a15 yeat
old.Please dont be harsh on me I still have to learn a lot.Thanks to
all once again.
 
S

sparky

Thanks everyone for all the help i just want to tell you all that you
all are professional people who know javascript thoroughly while i
have just started with javascript 2 days ago and am a15 yeat
old.Please dont be harsh on me I still have to learn a lot.Thanks to
all once again.

I changed the function name add to addup and it still did not work
though.
firebug showed that the event handler was not being fired.
I then specified the onClick handler in the makevis function like
this:

function makevis(form) {
form.out2.type="text";
form.add.type="hidden";
form.equalsadd.type="button";
form.equalsadd.onClick="addup(this.form)"; //added by me


This 'works' ( except that the output is a string as noted by
others...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top