473,614 Members | 2,328 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using 'name' or using 'id' of a form?

When using 'name' in the form, it works, when using 'id' it doesn't. Any
comments about this? By the way, is this a good method or is it better to
use 'getElementById '?
Carl

<body>
<form name="myform">
<input type button id="antw1" value="test">
</form>

<script type="text/javascript">
res=document.my form.antw1.valu e
alert(res)
</script>
Jul 20 '05 #1
4 13627

"Carl" wrote in message news:bj******** **@reader10.wxs .nl...
When using 'name' in the form, it works, when using 'id' it doesn't. Any comments about this? By the way, is this a good method or is it better to use 'getElementById '?
Carl

<body>
<form name="myform">
<input type button id="antw1" value="test">
</form>

<script type="text/javascript">
res=document.my form.antw1.valu e
alert(res)
</script>


I usually give my forms both a name and an ID (both the same).
Then i reference them by document.forms['myForm']
i.e. document.forms['myform'].antw1.value

getElementById( ) is the most recent method and is only available in IE5+
and NN6+.
It might be a good idea to get used to that method now, but make sure
you include and option for older browsers.
I.e.
d=document // to ease further scripting
if (myObject=d.get ElementById('my form')) {
res=myObject.an tw1.value
} else {
res=d.forms['myforms'].antw1.value
}

If you don't need the form tags you can access the objects directly
though;
res=antw1.value

Hope this helped.
--
B Alvestad
aka BraveBrain
Jul 20 '05 #2
"Børge Alvestad" wrote:
"Carl" wrote in message news:bj******** **@reader10.wxs .nl...
When using 'name' in the form, it works, when using 'id' it doesn't.

Any
comments about this? By the way, is this a good method or is it better

to
use 'getElementById '?
Carl

<body>
<form name="myform">
<input type button id="antw1" value="test">
</form>

<script type="text/javascript">
res=document.my form.antw1.valu e
alert(res)
</script>


I usually give my forms both a name and an ID (both the same).
Then i reference them by document.forms['myForm']
i.e. document.forms['myform'].antw1.value

getElementById( ) is the most recent method and is only available in IE5+
and NN6+.
It might be a good idea to get used to that method now, but make sure
you include and option for older browsers.
I.e.
d=document // to ease further scripting
if (myObject=d.get ElementById('my form')) {
res=myObject.an tw1.value
} else {
res=d.forms['myforms'].antw1.value
}

If you don't need the form tags you can access the objects directly
though;
res=antw1.value

Hope this helped.

--
B Alvestad
aka BraveBrain


document.forms['formName'].elements['elementName'] (or
document.forms['formName'].elementName) is still part of the standard.
There's no reason to test for and use getElementById( ). If getElementById( )
/is/ present, then you know you can still retrieve the value of a form
element using document.forms[...].elements[...].

As for giving your form elements the same NAME and ID. While it seems
economical and seems to work reliably across the majority of browsers,
recent discussions in this newsgroup have convinced me it's a bad practice.
This is especially true of things like checkboxes and radio buttons, which
can have several input instances with the same NAME, but each one should
have a unique ID.

As a result, I've begun using something like NAME="myInputNA ME"
ID="myInputID" (I haven't quite decided on the best naming scheme yet). For
groups of radio buttons, it's NAME="myRadioNA ME" ID="myRadioID1 " (or maybe
ID="myRadio1ID" , as I said, I haven't arrived at a conclusion as to what's
better).

Anyway, the point was, there's no reason to test for and use
getElementById( ). If it's supported, then so is
document.forms[...].elements[...].

--
| Grant Wagner <gw*****@agrico reunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 20 '05 #3
> When using 'name' in the form, it works, when using 'id' it doesn't. Any
comments about this? By the way, is this a good method or is it better to
use 'getElementById '?


'id' is used to access specific objects in the DOM. 'name', in form elements, is
used to fill in the names in the postdata that is generated when the form is
submitted. Early browsers tended to use one as the default for the other, which
led to general confusion about the roles of the attributes.

http://www.crockford.com/#javascript

Jul 20 '05 #4

Reply to "Grant Wagner", message
news:3F******** *******@agricor eunited.com...

Thanks for useful information, Grant ;)
--
Børge Alvestad
aka BraveBrain
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
13424
by: Chamomile | last post by:
I have been happily using array members as id's in my html code (either hand coded or generated by server-side script-php ) for some time. eg < input type='text' id='arrayItem' >< input type='text' id='arrayItem' >etc. particlarly in forms for sending input data to be server processed. and also using javascript client-side for dynamic text menu-trees etc. This has worked fine using IE Netscape and Opera as test browsers.
2
3443
by: Sam White | last post by:
I have set up a MySQL db on one server, IIS 6.0 on another. Using Frontpage I created some forms to input data. On a test page I made, I have 4 fields. First is the ID which is autonumber (I leave alone). The second is marked as TEXT, the third is MEDIUMTEXT, and the fourth is DATE. This is just an example though, the problem exists in no matter what manner I create the form. Now here is the issue. In the TEXT fields nothing is saved...
4
4742
by: Skully Matjas | last post by:
I am using the following code (created by the wizard) to allow to bring my form to a particular entery. But when I edit the entery (ex: put new information into a blank cell), it puts that record onto the bottom of the list (even though it keeps its record number). Also, There are certin names that i click on the list, and it will not bring it up, rather it brings to the first record (no matter how many times i try going to that...
1
4006
by: Daveyk0 | last post by:
Hello there, I have a front end database that I have recently made very many changes to to allow off-line use. I keep copies of the databases on my hard drive and link to them rather than the live databases on the network. Is there a way, via code, when I get back in-house from being on the road to click a button, and select the backends I want to link to? I would want to delete all the current links and link to the "live"
0
2196
by: Metal2You | last post by:
I'm working on an ASP.NET 2.0 application in Visual Studio 2005 that accesses a Sybase database back end. We're using Sybase SQL Anywhere 9.0.2.3228. I have installed and registered the Sybase .NET 2.0 DataProvider (iAnywhere.Data.AsaClient.dll) into the GAC so it can be used in the ProviderName property of a SQLDataSource and loads properly at run time. The application I'm writing is a bit more complex than the example I'm about to...
2
3147
by: shivendravikramsingh | last post by:
hi friends, i m using a ajax function for retrieving some values from a database table,and display the values in required field,my prob is that the ajax function i m using is working f9 once,but if i change something in php file using in ajax function.it not refreshed,means its shows the previous result it not get updated.i can't understand whats the prob.this is the code i m using: <? include("config.inc.php"); //error_reporting(0); ...
221
367325
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application needs to store entire files, the preferred method is to save the file onto the server’s file-system, and store the physical location of the file in your database. This is generally considered to be the easiest and fastest way to store files. ...
12
77976
lifeisgreat20009
by: lifeisgreat20009 | last post by:
I am a newbie to Struts and JSP...I have been working on the code below for 5 hours now..I googled a lot but couldn't get much help so finally I am here.. Hoping of getting my problem solved. Please give me some idea where I am going wrong ?? I just want to retrieve data from my emp_mstr table and display it using my JSP file... The table emp_mstr is as follows :- CREATE TABLE EMP_MSTR( EMP_NO VARCHAR(10) PRIMARY KEY, PASSWORD...
5
4455
by: thatcollegeguy | last post by:
Below are my 3php and 2js files. I create a table using ajax/php and then want to change the values in the tables add(+ number for teamid) id's for each specific td in the table. I don't know if I have the entirely wrong approach, but my code is below. Any or all help is appreciated! What currently happens is that the getdivision.php script works perfectly and gives me an entire table of correct data. The problem is that when I click...
0
8198
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
8642
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
8591
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...
0
8444
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7115
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...
0
5549
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4058
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2575
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
1758
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.