473,545 Members | 2,051 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 1474


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
2653
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
2045
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...
7
1857
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...
9
3010
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...
5
2970
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
1045
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...
1
1966
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...
36
3081
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
5532
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
2349
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...
0
7409
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...
0
7664
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. ...
0
7766
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5981
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...
1
5341
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1897
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
1
1022
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.