473,385 Members | 1,317 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,385 software developers and data experts.

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.myform.antw1.value
alert(res)
</script>
Jul 20 '05 #1
4 13610

"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.myform.antw1.value
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.getElementById('myform')) {
res=myObject.antw1.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.myform.antw1.value
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.getElementById('myform')) {
res=myObject.antw1.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="myInputNAME"
ID="myInputID" (I haven't quite decided on the best naming scheme yet). For
groups of radio buttons, it's NAME="myRadioNAME" 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*****@agricoreunited.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***************@agricoreunited.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
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...
2
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...
4
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...
1
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...
0
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...
2
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...
221
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...
12
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...
5
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.