Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #12454

Referring to Controls by id

From Gene Wirchenko <genew@ocis.net>
Newsgroups comp.lang.javascript
Subject Referring to Controls by id
Date 2012-03-06 14:03 -0800
Organization A noiseless patient Spider
Message-ID <191dl7ltuts723trrgoj29mln2b3uvqflk@4ax.com> (permalink)

Show all headers | View raw


Dear JavaScripters:

     Given a form with name=theForm and a control with
name=theControl, I can refer to the control with
          document.theForm.theControl

     If, instead of giving the control that name, I give it that id,
the reference still works, but if I change the form the same way
(giving it an id instead of a name of theForm, the reference fails
with the error:
          Error: Unable to get value of the property 'theControl':
object is null or undefined

     Why?

     I suppose I will just change everything to use
document.getElementById(), but the above is shorter.

***** Start of Test Page *****
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
  <title>2012-03-06: Change name to id</title>
</head>

<body>

<script type="text/javascript">
function DoThis()
  {
  document.theForm.theControl.value="Changed";
  }
</script>

<form id=theForm>
  <input name=One value="Start One" onblur="return DoThis();">
  <br>
  <input name=Two value="Start Two">
  <br>
  <input id=theControl value="Start Three">
  <br>
</form>

</body>

</html>
***** End of Test Page *****

Sincerely,

Gene Wirchenko

Back to comp.lang.javascript | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Referring to Controls by id Gene Wirchenko <genew@ocis.net> - 2012-03-06 14:03 -0800
  Re: Referring to Controls by id Jeff North <jnorthau@yahoo.com.au> - 2012-03-07 09:23 +1100
    Re: Referring to Controls by id Gene Wirchenko <genew@ocis.net> - 2012-03-06 15:02 -0800
      Re: Referring to Controls by id Jeff North <jnorthau@yahoo.com.au> - 2012-03-07 19:08 +1100
        Re: Referring to Controls by id "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2012-03-07 10:44 +0200
          Re: Referring to Controls by id Jeff North <jnorthau@yahoo.com.au> - 2012-03-07 23:32 +1100
      Re: Referring to Controls by id Dr J R Stockton <reply1210@merlyn.demon.co.uk> - 2012-03-07 21:26 +0000
  Re: Referring to Controls by id Denis McMahon <denismfmcmahon@gmail.com> - 2012-03-07 01:31 +0000
    Re: Referring to Controls by id Gene Wirchenko <genew@ocis.net> - 2012-03-06 22:33 -0800
      Re: Referring to Controls by id Dr J R Stockton <reply1210@merlyn.demon.co.uk> - 2012-03-08 19:06 +0000
    Re: Referring to Controls by id "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-03-07 10:26 +0000
      Re: Referring to Controls by id Gene Wirchenko <genew@ocis.net> - 2012-03-07 08:55 -0800
        Re: Referring to Controls by id "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-03-08 09:44 +0000
          Re: Referring to Controls by id Gene Wirchenko <genew@ocis.net> - 2012-03-08 19:26 -0800

csiph-web