By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,264 Members | 1,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,264 IT Pros & Developers. It's quick & easy.

Help, I am new to Javascript

P: n/a
Here is a stripped, simple code for input of 2 numbers, and getting
their sum.

<HEAD>
<script language="javascript">
function adder() {
a=parseFloat(document.nform.ainp.value);
b=parseFloat(document.nform.binp.value);
document.nform.sinp.value=a+b;
}
</script>
</HEAD>

<BODY>
<form action="" method="post" name="nform" >
a <input name="ainp" type="text" size="9">
b <input name="binp" type="text" size="9">
sum <input name="sinp" type="text" size="9" disabled>
<input type="button" value="Add" onClick="adder()">
</form>
</BODY>
</HTML>

My question is:
If I put the inputs into e.g. a table, the function still works, with
the "document.nform.ainp.value" notation.

<BODY>
<form action="" method="post" name="nform" >
<table>
<tr><td>
a <input name="ainp" type="text" size="9">
b <input name="binp" type="text" size="9">
sum <input name="sinp" type="text" size="9" disabled>
<input type="button" value="Add" onClick="adder()">
</td></tr>
</table>
</form>
</BODY>

I would think that the <table>, <tr> and <td> should have names or id's
interferring into that hierarcical notation?

Regards knos

Mar 10 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a


knos wrote:

a=parseFloat(document.nform.ainp.value);
b=parseFloat(document.nform.binp.value);
document.nform.sinp.value=a+b;
If I put the inputs into e.g. a table, the function still works, with
the "document.nform.ainp.value" notation.
I would think that the <table>, <tr> and <td> should have names or id's
interferring into that hierarcical notation?


No, what you use there is nowadays called DOM Level 0 from a time when
browsers did not make all elements scriptable, only certain elements
like img elements or form elements and the control elements inside of
forms. Those object access methods alike
document.formName.formControlName
or
document.forms.formName.elements.formControlName
do not change when additional child elements are included, the control
(e.g. input, textarea, select) is part of exactly one form element
object in that model and the depth of descendants does not matter.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Mar 10 '06 #2

P: n/a
Thanks for your answer!
You say I use DOM Level 0. I thought the DOM model was built in and
spesific to the browser? I have tested with Mozilla 1.7.5 and Internet
Explorer 6.0. I would think that these browsers used a newer DOM?
And what if someone using a browser with newer DOM would read my code?
Then the table would interfere so that the function could not work?

Still confused
knos

Mar 13 '06 #3

P: n/a
knos wrote:
Thanks for your answer!
You say I use DOM Level 0. I thought the DOM model was built in and
spesific to the browser?
No. There are standards (with different levels of support and various
proprietry extensions)
I have tested with Mozilla 1.7.5 and Internet Explorer 6.0.
I would think that these browsers used a newer DOM?
Supporting DOM 1 doesn't mean they can't also support DOM 0.
And what if someone using a browser with newer DOM would read my code?
Then the table would interfere so that the function could not work?


No. DOM 0 isn't likely to be changed (nor is DOM 1, nor DOM 2, etc).

Mar 13 '06 #4

P: n/a
My browsers do not accept this:

<script language="javascript">
function adder() {
a=parseFloat(document.nform.Tab.Row.Cell.ainp.valu e);
b=parseFloat(document.nform.Tab.Row.Cell.binp.valu e);
document.nform.Tab.Row.Cell.sinp.value=a+b;
}
</script>

<BODY>
<form action="" method="post" name="nform" >
<table id="Tab"><tr id="Row"><td id="Cell">
a <input name="ainp" type="text" size="9">
b <input name="binp" type="text" size="9">
sum <input name="sinp" type="text" size="9" disabled>
<input type="button" value="Add" onClick="adder()">
</td></tr></table>
</form>
</BODY>

Is'nt this what DOM level 1 ought to accept?
If I remove the refereces to the table, following works:

function adder() {
a=parseFloat(document.nform.ainp.value);
b=parseFloat(document.nform.binp.value);
document.nform.sinp.value=a+b;
}

Mar 13 '06 #5

P: n/a
knos said the following on 3/13/2006 10:55 AM:
My browsers do not accept this:
I would be shocked if *any* browser accepted that code.
<script language="javascript">
function adder() {
a=parseFloat(document.nform.Tab.Row.Cell.ainp.valu e);
b=parseFloat(document.nform.Tab.Row.Cell.binp.valu e);
document.nform.Tab.Row.Cell.sinp.value=a+b;
}
</script>

<BODY>
<form action="" method="post" name="nform" >
<table id="Tab"><tr id="Row"><td id="Cell">
a <input name="ainp" type="text" size="9">
b <input name="binp" type="text" size="9">
sum <input name="sinp" type="text" size="9" disabled>
<input type="button" value="Add" onClick="adder()">
</td></tr></table>
</form>
</BODY>

Is'nt this what DOM level 1 ought to accept?
no.
If I remove the refereces to the table, following works:

function adder() {
a=parseFloat(document.nform.ainp.value);
b=parseFloat(document.nform.binp.value);
document.nform.sinp.value=a+b;
}


That's because you are now using the forms collection.
document.formNameorID.elementName.property
Read the FAQ with regards to bracket notation.

function adder(){
a = +document.forms['nform'].elements['ainp'].value;
b = +document.forms['nform'].elements['binp'].value;
document.forms['nform'].elements['sinp'].value=a+b;
}

Also, you might want to read the Best Practices document in my signature.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 13 '06 #6

P: n/a
I found out that the table always should have a <TBODY>, but this did
not help either. I also think maybe only DOM level 2 makes objects for
the table, see:
http://www.w3.org/TR/DOM-Level-2-Core/introduction.html
Then if Mozilla 1.7.5 and Internet Explorer 6.0 does not support level
2, things begin to clear. But if it is that simple, somebody would have
told me before?

knos

Mar 14 '06 #7

P: n/a
knos said the following on 3/14/2006 8:07 AM:

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.

<URL: http://www.safalra.com/special/googlegroupsreply/ >
I found out that the table always should have a <TBODY>, but this did
not help either.
It won't make a difference if you are accessing form elements as the
TBODY has nothing at all to do with Forms other than that maybe a form
is a descendant of the TBODY in the DOM Tree.
I also think maybe only DOM level 2 makes objects for the table, see:
http://www.w3.org/TR/DOM-Level-2-Core/introduction.html
It's irrelevant.
Then if Mozilla 1.7.5 and Internet Explorer 6.0 does not support level
2, things begin to clear.
The do support Dom Level 2.
But if it is that simple, somebody would have told me before?

Tell you what? That to access a form it doesn't matter how many tbody,
tr or td tags are in the page? It's irrelevant.

<form id="myForm">
<input type="text" name="myInput" value="This is myInput's value">
</form>

<button
onclick="alert(document.forms['myForm'].elements['myInput'].value">
Click this button to get the inputs value - all without using any DOM 2
TBODY nonsense crap to try to access it
</button>

Now, wrap that code in any amount of table code you want to and it still
won't change the code used to access a form element. Did you bother
reading the references I pointed you to?

<URL: http://jibbering.com/faq/#FAQ4_13>
Titled: How do I get the value of a form control?

Read it. Read it twice. Had you done that to start with, you wouldn't
have the questions you are asking.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 14 '06 #8

P: n/a
It won't make a difference if you are accessing form elements as the
TBODY has nothing at all to do with Forms other than that maybe a form
is a descendant of the TBODY in the DOM Tree.
Yes, the form ought to be a descendant of the TD which would be a
descendant of the TR which is a descendant of TBODY which is a
descendant of TABLE. Remember that the form is in the table, not the
other way around.
Then I find it weard that you can ignore the table, if the DOM has made
objects of the TABLE, TBODY, TR and TD.

: Now, wrap that code in any amount of table code you want to and it still
won't change the code used to access a form element. Did you bother
reading the references I pointed you to?


As said above, the table is in the form, not the other way around. But
if the DOM has made objects of TABLE, TBODY, TR and TD, it ought to
matter even if the form was in the table. The form would be a
descendant of the TD. There is something fundamental that I do'nt
understand, and you maybe take for granted. I have read the references,
and cannot find anything relevant there.

knos

Mar 14 '06 #9

P: n/a
knos wrote:
It won't make a difference if you are accessing form elements as the
TBODY has nothing at all to do with Forms other than that maybe a form
is a descendant of the TBODY in the DOM Tree.


Yes, the form ought to be a descendant of the TD which would be a
descendant of the TR which is a descendant of TBODY which is a
descendant of TABLE.


Which (as has been mentioned several times already) is entirely irrelevant
if you are accessing the form via the forms collection and form controls
via the elements collection (or element accessor properties[1]) of that
form.

document.nform.ainp.value means 'The value property of the *form* *control*
with the name or id "ainp" that is inside the *form* with the name or id
"nform"'.

It does *NOT* mean "The value of the element with name or id of "aimp" that
is a *child* of the element with name or id of 'nform'.
[1] My terminology might be wrong here.
--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Mar 14 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.