Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #7634

Re: What is wrong with this code?its not working

Newsgroups comp.lang.javascript
Subject Re: What is wrong with this code?its not working
References <55a344ce-8a2b-494a-bfe8-fadd3a5e2e08@h30g2000pro.googlegroups.com>
From Lasse Reichstein Nielsen <lrn.unread@gmail.com>
Date 2011-10-23 11:46 +0200
Message-ID <wrbwkpl6.fsf@gmail.com> (permalink)
Organization TDC Totalloesninger

Show all headers | View raw


Mclaren Fan <himanshu1495@gmail.com> writes:

> <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'
-- 
Lasse Reichstein Holst Nielsen
 'Javascript frameworks is a disruptive technology'
  

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

What is wrong with this code?its not working Mclaren Fan <himanshu1495@gmail.com> - 2011-10-22 23:33 -0700
  Re: Please google for a FAQ on asking questions Andreas Bergmaier <andber93@web.de> - 2011-10-23 10:52 +0200
  Re: What is wrong with this code?its not working Lasse Reichstein Nielsen <lrn.unread@gmail.com> - 2011-10-23 11:46 +0200
  Re: What is wrong with this code?its not working John G Harris <john@nospam.demon.co.uk> - 2011-10-23 11:25 +0100
    Re: What is wrong with this code?its not working Dr J R Stockton <reply1143@merlyn.demon.co.uk> - 2011-10-24 20:05 +0100
  Re: What is wrong with this code?its not working Denis McMahon <denismfmcmahon@gmail.com> - 2011-10-25 01:55 +0000
    Re: What is wrong with this code?its not working Dr J R Stockton <reply1143@merlyn.demon.co.uk> - 2011-10-26 22:21 +0100
      Re: What is wrong with this code?its not working Denis McMahon <denismfmcmahon@gmail.com> - 2011-10-26 23:34 +0000
        Re: What is wrong with this code?its not working Mclaren Fan <himanshu1495@gmail.com> - 2011-10-27 01:26 -0700
          Re: What is wrong with this code?its not working sparky <simonp99@googlemail.com> - 2011-10-27 09:46 -0700

csiph-web