473,698 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help, I am new to Javascript

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

<HEAD>
<script language="javas cript">
function adder() {
a=parseFloat(do cument.nform.ai np.value);
b=parseFloat(do cument.nform.bi np.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 1492


knos wrote:

a=parseFloat(do cument.nform.ai np.value);
b=parseFloat(do cument.nform.bi np.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.formNa me.formControlN ame
or
document.forms. formName.elemen ts.formControlN ame
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="javas cript">
function adder() {
a=parseFloat(do cument.nform.Ta b.Row.Cell.ainp .value);
b=parseFloat(do cument.nform.Ta b.Row.Cell.binp .value);
document.nform. Tab.Row.Cell.si np.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(do cument.nform.ai np.value);
b=parseFloat(do cument.nform.bi np.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="javas cript">
function adder() {
a=parseFloat(do cument.nform.Ta b.Row.Cell.ainp .value);
b=parseFloat(do cument.nform.Ta b.Row.Cell.binp .value);
document.nform. Tab.Row.Cell.si np.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(do cument.nform.ai np.value);
b=parseFloat(do cument.nform.bi np.value);
document.nform. sinp.value=a+b;
}


That's because you are now using the forms collection.
document.formNa meorID.elementN ame.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.javas cript 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.c om, 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.javas cript 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
2661
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 gethelp(nn) where nn is the helpid. The function is contained in a header file called funcs.inc that each page
5
2055
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, Previous Slide and Home Slide. Is it possible for you to view my page and tell me what I am doing wrong. I have looked at this page for hours and can't figure it out, I
7
1865
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 at validator.w3.org I get a error the attribute given above is required for an element that you've used, but you have omitted it. For instance, in most HTML and XHTML document types the "type" attribute is required on the "script" element and the...
9
3032
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 all, or works sporadically. I'm talking about things like Hotmail, Yahoo Address Book, buttons on various sites, etc.. I'm a computer person, but not a Web OR Javascript person. Is there any way I can "start over again?" Can I, a Windows 98...
5
2994
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 <<<<<<<<<<<<<<CODE>>>>>>>>>>>>>>>> <html>
0
1053
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 intend to use in server side code.As I am using a javascript library so I have placed necessary javascript files in the project directory. I've placed a button on the webform and I would like to invoke following javascript code on every button click
1
1983
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 HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <%@ taglib uri="/WEB-INF/tag-lib/struts-html-el.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/tag-lib/struts-bean.tld" prefix="bean" %>
36
3098
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 highlights this line: <form name="frmCurrency" action="" method="post"> Page source....
0
5569
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 ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
7
2367
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 date from calender and clicks search button it will fetch data from database. But what I need is as soon the user clicks the date, it should fetch data for that date from database. How to do it. Here is the code I am using <html> <script...
0
8671
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8598
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9152
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9016
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8887
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7709
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
3037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2321
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.