473,407 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Help, I am new to Javascript

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
9 1470


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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Edward King | last post by:
Hi! I am trying to achieve the following: I have a number of help pages (in the format help_nn.php where nn=helpid). I want to be able to open a particular help page by calling the function...
5
by: TrvlOrm | last post by:
HI There, I have been struggling with JavaScript code for days now, and this is my last resort! Please help... I am trying to create a JavaScript slide show with links for Next Slide,...
7
by: mike | last post by:
Hello, I am kind of new to this javascript stuff and I am constantly having problems trying to get my webpage validated. I have the following <script>printdate();</script> and when I validate it...
9
by: YZK | last post by:
Hello. I'm not a Web developer, just a user, and I think I may have somehow messed myself up majorly. I'm not quite sure how. Right now, javascript used by websites I go to either does not work at...
5
by: Craig Keightley | last post by:
Please help, i have attached my page which worksin IE but i cannnot get the drop down menu to fucntion in firefox. Any one have any ideas why? Many Thanks Craig ...
0
by: Erland | last post by:
Hello All, I am using AJAX library( AjaxEngine from http://www.mathertel.de/AJAXEngine/) and having problems and hope someone here can help me. I have some piece of javascript code that I...
1
by: JumpingOffPlace | last post by:
Hi, I'm hoping that the wealth of knowledge here can stop me from spinning my wheels on this syntax error for hours. :) Below is the code, and the error I am recieving.... Code: <!DOCTYPE...
36
by: aljamala | last post by:
Hi, I keep getting this warning on a page, but I do not know what the problem is...does anyone have an idea about what could be wrong? line 88 column 7 - Warning: missing </formbefore <td> it...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
7
by: gubbachchi | last post by:
Hi all, In my application I need to display the data fetched from mysql database after the user selects date from javascript calender. I have written the code in which after the user selects the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.