Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #7630 > unrolled thread
| Started by | Mclaren Fan <himanshu1495@gmail.com> |
|---|---|
| First post | 2011-10-22 23:33 -0700 |
| Last post | 2011-10-27 09:46 -0700 |
| Articles | 10 — 7 participants |
Back to article view | Back to comp.lang.javascript
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
| From | Mclaren Fan <himanshu1495@gmail.com> |
|---|---|
| Date | 2011-10-22 23:33 -0700 |
| Subject | What is wrong with this code?its not working |
| Message-ID | <55a344ce-8a2b-494a-bfe8-fadd3a5e2e08@h30g2000pro.googlegroups.com> |
<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>
[toc] | [next] | [standalone]
| From | Andreas Bergmaier <andber93@web.de> |
|---|---|
| Date | 2011-10-23 10:52 +0200 |
| Subject | Re: Please google for a FAQ on asking questions |
| Message-ID | <j80kkf$3g3$1@news.albasani.net> |
| In reply to | #7630 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Lasse Reichstein Nielsen <lrn.unread@gmail.com> |
|---|---|
| Date | 2011-10-23 11:46 +0200 |
| Message-ID | <wrbwkpl6.fsf@gmail.com> |
| In reply to | #7630 |
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'
[toc] | [prev] | [next] | [standalone]
| From | John G Harris <john@nospam.demon.co.uk> |
|---|---|
| Date | 2011-10-23 11:25 +0100 |
| Message-ID | <UcRpxzEUu+oOFwh1@J.A830F0FF37FB96852AD08924D9443D28E23ED5CD> |
| In reply to | #7630 |
On Sat, 22 Oct 2011 at 23:33:35, in comp.lang.javascript, Mclaren Fan
wrote:
><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
--
John Harris
[toc] | [prev] | [next] | [standalone]
| From | Dr J R Stockton <reply1143@merlyn.demon.co.uk> |
|---|---|
| Date | 2011-10-24 20:05 +0100 |
| Message-ID | <D7If3ML+bbpOFwZe@invalid.uk.co.demon.merlyn.invalid> |
| In reply to | #7636 |
In comp.lang.javascript message <UcRpxzEUu+oOFwh1@J.A830F0FF37FB96852AD0 8924D9443D28E23ED5CD>, Sun, 23 Oct 2011 11:25:24, John G Harris <john@nospam.demon.co.uk> posted: >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.) -- (c) John Stockton, near London. *@merlyn.demon.co.uk/?.?.Stockton@physics.org Web <http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, and links. Correct <= 4-line sig. separator as above, a line precisely "-- " (RFC5536/7) Do not Mail News to me. Before a reply, quote with ">" or "> " (RFC5536/7)
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2011-10-25 01:55 +0000 |
| Message-ID | <4ea616f4$0$28474$a8266bb1@newsreader.readnews.com> |
| In reply to | #7630 |
On Sat, 22 Oct 2011 23:33:35 -0700, Mclaren Fan wrote:
> 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
[toc] | [prev] | [next] | [standalone]
| From | Dr J R Stockton <reply1143@merlyn.demon.co.uk> |
|---|---|
| Date | 2011-10-26 22:21 +0100 |
| Message-ID | <PQAs9OURnHqOFwC+@invalid.uk.co.demon.merlyn.invalid> |
| In reply to | #7675 |
In comp.lang.javascript message <4ea616f4$0$28474$a8266bb1@newsreader.re
adnews.com>, Tue, 25 Oct 2011 01:55:00, Denis McMahon
<denismfmcmahon@gmail.com> posted:
>On Sat, 22 Oct 2011 23:33:35 -0700, Mclaren Fan wrote:
>
>> 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.
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[S].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.
--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms and links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2011-10-26 23:34 +0000 |
| Message-ID | <4ea89904$0$28587$a8266bb1@newsreader.readnews.com> |
| In reply to | #7709 |
On Wed, 26 Oct 2011 22:21:21 +0100, Dr J R Stockton wrote: > 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
[toc] | [prev] | [next] | [standalone]
| From | Mclaren Fan <himanshu1495@gmail.com> |
|---|---|
| Date | 2011-10-27 01:26 -0700 |
| Message-ID | <fa2162ee-2af7-48f0-95d3-02eb2d8f9714@s32g2000prj.googlegroups.com> |
| In reply to | #7710 |
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.
[toc] | [prev] | [next] | [standalone]
| From | sparky <simonp99@googlemail.com> |
|---|---|
| Date | 2011-10-27 09:46 -0700 |
| Message-ID | <fa2715ed-7056-40ec-98fd-6ef0706e2606@g7g2000vbv.googlegroups.com> |
| In reply to | #7717 |
On Oct 27, 9:26 am, Mclaren Fan <himanshu1...@gmail.com> wrote:
> 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...
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web