Re: Best Way to access Input fields
- From: Randy Webb <HikksNotAtHome@xxxxxxx>
- Date: Fri, 20 Jan 2006 17:54:28 -0500
Dave Anderson said the following on 1/20/2006 3:58 PM:
Praveen wrote:var InpObj= document.getElementByName("FrmInput")
There is no such method. "Element" is singular when sought by id, and plural when sought by name:
container.getElementById(...) container.getElementsByName(...)
Recently I came to know that following is a better way to access objects.
var InpObj= document.Frm.elements["FrmInput"] .
I don't know if I consider that better -- it is the old DOM Level 0 way of doing it, which means it will work with virtually any browser. But virtually any browser can do it with the new DOM methods as well.
Depending on how the HTML is defined.
<input name="myInput">
myInputValue = document.getElementById('myInput').value;will only work in IE as any other browser won't use the ID in place of the NAME.
Any modern browser will pick it up this way:
<input id="myInput">
myInputValue = document.getElementById('myInput').value;But, Form Elements with an ID but no NAME don't get submitted to the server. So, if you are going to submit the form and use the NAME attribute, then you are better off (simpler) to use the forms collection:
document.forms['formNameorID'].elements['elementNAMEnotID'].value;
I changed my previous way of posting that from formNAMEnotID due to a discussion with Lasse about ID's and NAME's on Form Elements.
-- Randy comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/ .
- Follow-Ups:
- Re: Best Way to access Input fields
- From: Dave Anderson
- Re: Best Way to access Input fields
- References:
- Re: Best Way to access Input fields
- From: Dave Anderson
- Re: Best Way to access Input fields
- Prev by Date: Re: Best Way to access Input fields
- Next by Date: Re: Changing src= in <script>
- Previous by thread: Re: Best Way to access Input fields
- Next by thread: Re: Best Way to access Input fields
- Index(es):
Relevant Pages
|