473,385 Members | 1,908 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.

Keep getting undefined variable error

Hi!

Any ideas as to how I can get ride of the script error undefined
variable "Exp_Month"?

I am trying to get this drop down list to default to the current month
as opposed to 01.

Thank you in advance.

<SELECT size="1" name="Exp_Month">
<OPTION value="01" selected>1</OPTION>
<OPTION value="02">2</OPTION>
<OPTION value="03">3</OPTION>
<OPTION value="04">4</OPTION>
<OPTION value="05">5</OPTION>
<OPTION value="06">6</OPTION>
<OPTION value="07">7</OPTION>
<OPTION value="08">8</OPTION>
<OPTION value="09">9</OPTION>
<OPTION value="10">10</OPTION>
<OPTION value="11">11</OPTION>
<OPTION value="12">12</OPTION>
</SELECT>

<script type="text/javascript">
var now = new Date()
Exp_Month.selectedIndex = now.getMonth()
</script>
Jul 20 '05 #1
10 2289
LJL
se*********@cfl.rr.com (Chuck) wrote in
news:85**************************@posting.google.c om:
<SELECT size="1" name="Exp_Month">
<OPTION value="01" selected>1</OPTION>
<OPTION value="02">2</OPTION>
<OPTION value="03">3</OPTION>
<OPTION value="04">4</OPTION>
<OPTION value="05">5</OPTION>
<OPTION value="06">6</OPTION>
<OPTION value="07">7</OPTION>
<OPTION value="08">8</OPTION>
<OPTION value="09">9</OPTION>
<OPTION value="10">10</OPTION>
<OPTION value="11">11</OPTION>
<OPTION value="12">12</OPTION>
</SELECT>

<script type="text/javascript">
var now = new Date()
Exp_Month.selectedIndex = now.getMonth()
</script>


The easiest way I have found, is to change the script to:

<script type="text/javascript">
var now = new Date();
document.getElementById('Exp_Month').selectedIndex = now.getMonth();
</script>
Alternatively, name the form. If named "form_name", call the element by:

document.form_name.Exp_Month. selectedIndex = now.getMonth();

One last thing, which you probably know . . .
Be aware that months are zero-based, January is zero, Februaryis one,
etc. So calling the element's selectedIndex works, because the options are
zero-based liked the months. However, your values are 1-12. If you plan
to use the element's values, you may want to renumber them 0-11.
It only makes a difference if you are going to use the value
information for some month-related/date related script function in the
future.

Good luck,
LJL

Jul 20 '05 #2
On Mon, 09 Feb 2004 04:51:46 GMT, LJL <no**@nowhere.com> wrote:

[snip]
The easiest way I have found, is to change the script to:

<script type="text/javascript">
var now = new Date();
document.getElementById('Exp_Month').selectedIndex = now.getMonth();


I beg your pardon? You cannot use document.getElementById with a named
element. Yes, it works in IE, but it won't work in all the other browsers.
document.getElementById should be used with ID'd elements. If you want to
get a named element using the DOM, use document.getElementsByName.

If you are trying to access a named element within a form, use[1]:

document.formName.Exp_Month.selectedIndex

If you don't use a form, ID the select element.

Be aware that the DOM is not supported well (or at all) by all browsers.
You should use feature detection to determine support for a particular
method. The example below assumes you ID'd the element, Exp_Month.

var expMonth = null;

if( document.getElementById ) {
expMonth = document.getElementById( 'Exp_Month' );
} else if( document.all ) {
expMonth = document.all[ 'Exp_Month' ];
}

expMonth.selectedIndex = .....

[snip]

Mike
[1] You can also use a collection-oriented accessor method. It is slightly
slower and more to type, but can be used to access ID'd elements, and
elements with names that include characters that are interpreted as
operators in JavaScript (+, -, [, ], *, etc). The same expression would be
written as:

document.forms['formName'].elements['Exp_Month'].selectedIndex

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #3
On Mon, 09 Feb 2004 08:44:58 GMT, Michael Winter
<M.******@blueyonder.co.invalid> wrote:

[snip]
var expMonth = null;

if( document.getElementById ) {
expMonth = document.getElementById( 'Exp_Month' );
} else if( document.all ) {
expMonth = document.all[ 'Exp_Month' ];
}

expMonth.selectedIndex = ...


This example should end

if( expMonth ) {
expMonth.selectedIndex = ...
}

[snip]

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #4
Michael Winter wrote:
On Mon, 09 Feb 2004 04:51:46 GMT, LJL <no**@nowhere.com> wrote:

[snip]
The easiest way I have found, is to change the script to:

<script type="text/javascript">
var now = new Date();
document.getElementById('Exp_Month').selectedIndex = now.getMonth();

I beg your pardon? You cannot use document.getElementById with a named
element. Yes, it works in IE, but it won't work in all the other
browsers. document.getElementById should be used with ID'd elements. If
you want to get a named element using the DOM, use
document.getElementsByName.


Well, since the name attribute is deprecated anyway, it is probably best
to switch to the id attribute. Or, include both, with the same value.

Recently I read that IE indeed allows element reference by id (no, that
should be: name) immediately, but that makes for hard readable code
unless you are well versed in the document structure. Therefore I think
document.getElementById is the better way. I wish they'd chosen a
shorter spelling...
Be aware that the DOM is not supported well (or at all) by all browsers.
You should use feature detection to determine support for a particular
method.


I agree.

--
Bas Cost Budde
http://www.heuveltop.org/BasCB
but the domain is nl

Jul 20 '05 #5
"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:op**************@news-text.blueyonder.co.uk...
<snip>
document.formName.Exp_Month.selectedIndex <snip>[1] You can also use a collection-oriented accessor method. It is
slightly slower and more to type, but can be used to access ID'd
elements, and elements with names that include characters that
are interpreted as operators in JavaScript (+, -, [, ], *, etc).
The same expression would be written as:

document.forms['formName'].elements['Exp_Month'].selectedIndex


It is not the use of the collections that allows the "illegal"
characters to be used in javascript property accessors it is the square
bracket notation that makes it possible. The property names of an (any)
object are unrestricted by ECMA 262 (probably there are implementation
imposed limits, such as total property name length). It is the dot
notation accessors that impose a restriction on the names used because
the items between the dots must conform to the production rules for an
identifier.

As the two forms of property accessor syntax are equivalent (in terms of
how they work) it is possible to use square bracket notation wherever
dot notation is used, so the above shortcut form accessor could be any
of (and more):-

document["formName"].Exp_Month.selectedIndex
document.formName["Exp_Month"].selectedIndex
document["formName"]["Exp_Month"].selectedIndex
document["formName"]["Exp_Month"]["selectedIndex"]
window["document"]["formName"]["Exp_Month"]["selectedIndex"]

- and the strings used in the bracket notation can hold any character
sequence.

Similarly the collections based assessors could use dot notation
(assuming the names/IDs conformed with the identifier rules):-

document.forms.formName.elements.Exp_Month.selecte dIndex

The reason that I use the longer collections based accessors with the
form and control names in bracket notation is for the clarity it
provides in the source code. The collections access leaves a reader of
the code in no doubt that the subject of the accessor is a control
within a form (as they can clearly read "forms" and "elements" in the
accessor). And the use of bracket notation in that context separates the
HTML namespace from the javascript namespace.

Speed of resolution of the longer accessors is not usually an issue as I
would assign a reference to the - elements - collection to a local
variable anyway:-

var els = document.forms['formName'].elements;

- and then refer to the controls relative to that, so most of the work
resolving the form accessors is only done once. (it also makes the
amount of extra typing involved when using the longer form
insignificant)

Richard.
Jul 20 '05 #6
Bas Cost Budde <ba*@heuveltop.org> writes:
Well, since the name attribute is deprecated anyway, it is probably
best to switch to the id attribute.
It is not deprecated on form controls. The example that is currently
discussed is for a select element, i.e., a form control (if inside
a form, at least).
Or, include both, with the same value.


When used on form controls, name and id values doesn't have to be
identical. The id value gives the anchor name of the object, the name
value gives the control name (the one that is used when the form is
submitted).

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #7
On Mon, 9 Feb 2004 17:46:40 -0000, Richard Cornford
<Ri*****@litotes.demon.co.uk> wrote:
"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:op**************@news-text.blueyonder.co.uk...
<snip>
document.formName.Exp_Month.selectedIndex <snip>
[1] You can also use a collection-oriented accessor method. It is
slightly slower and more to type, but can be used to access ID'd
elements, and elements with names that include characters that
are interpreted as operators in JavaScript (+, -, [, ], *, etc).
The same expression would be written as:

document.forms['formName'].elements['Exp_Month'].selectedIndex


It is not the use of the collections that allows the "illegal"
characters to be used in javascript property accessors it is the square
bracket notation that makes it possible.


I didn't say that, nor did I (intend to) imply it.

[snip]
document["formName"].Exp_Month.selectedIndex
document.formName["Exp_Month"].selectedIndex
document["formName"]["Exp_Month"].selectedIndex
document["formName"]["Exp_Month"]["selectedIndex"]
window["document"]["formName"]["Exp_Month"]["selectedIndex"]

- and the strings used in the bracket notation can hold any character
sequence.

Similarly the collections based assessors could use dot notation
(assuming the names/IDs conformed with the identifier rules):-

document.forms.formName.elements.Exp_Month.selecte dIndex


I've seen collections accessed like that, but it's usually been in badly
written, IE-only scripts so I've always ignored it. I never realised that
the bracket- and dot-notations were quite so interchangable.

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #8
LJL
Michael Winter <M.******@blueyonder.co.invalid> wrote in
news:op**************@news-text.blueyonder.co.uk:
I beg your pardon? You cannot use document.getElementById with a named
element. Yes, it works in IE, but it won't work in all the other
browsers. document.getElementById should be used with ID'd elements.
If you want to get a named element using the DOM, use
document.getElementsByName.

[snip]

Mike

You're absolutely right. My apologies. I had added the ID tag to my test
bed when I tried the mod to make sure it worked. I forgot completely about
having done so when I sent my reply to the original post.

Thanks for pointing out my shortfall.

LJL
Jul 20 '05 #9
I have read the following message from se*********@cfl.rr.com (Chuck)
and have decided to lend my vast knowledge.

The writer said:
Hi!

Any ideas as to how I can get ride of the script error undefined
variable "Exp_Month"?

I am trying to get this drop down list to default to the current month
as opposed to 01.

Thank you in advance.

<SELECT size="1" name="Exp_Month">
<OPTION value="01" selected>1</OPTION>
<OPTION value="02">2</OPTION>
<OPTION value="03">3</OPTION>
<OPTION value="04">4</OPTION>
<OPTION value="05">5</OPTION>
<OPTION value="06">6</OPTION>
<OPTION value="07">7</OPTION>
<OPTION value="08">8</OPTION>
<OPTION value="09">9</OPTION>
<OPTION value="10">10</OPTION>
<OPTION value="11">11</OPTION>
<OPTION value="12">12</OPTION>
</SELECT>

<script type="text/javascript">
var now = new Date()
Exp_Month.selectedIndex = now.getMonth()
</script>


and my reply is:
I'm not sure but try putting the select within a form.

--
Dennis M. Marks
http://www.dcs-chico.com/~denmarks/
Replace domain.invalid with dcsi.net
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 20 '05 #10
Lasse Reichstein Nielsen wrote:
Bas Cost Budde <ba*@heuveltop.org> writes:
Well, since the name attribute is deprecated anyway, it is probably
best to switch to the id attribute.


It is not deprecated on form controls.


You're right.
Or, include both, with the same value.


When used on form controls, name and id values doesn't have to be
identical. The id value gives the anchor name of the object, the name
value gives the control name (the one that is used when the form is
submitted).


Thanks. I had trouble using getElementById in IE since that also
returned matches on the name attribute, but I probably gave up on the
whole name attribute too early.
--
Bas Cost Budde
http://www.heuveltop.org/BasCB
but the domain is nl

Jul 20 '05 #11

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

Similar topics

3
by: Jason | last post by:
hello, i am new to PHP, so go easy. I am using the examples in the book: PHP: Your Visual Blueprint For Creating Open Source, Server Side Content In the section where they talk about...
7
by: Coder Droid | last post by:
I decided to run some code with errors set to E_ALL, just to see what I would run across. It caught a few things, but 90% or better of the messages were of the 'undefined' kind: PHP Notice: ...
11
by: mikey_boy | last post by:
Hello! Curious if anyone could give me a hand. I wrote this PHP script with makes a simple connection to a mysql database called firstdb and just pulls back the results and displays on the...
10
by: Sharon | last post by:
Hi! Does anyone know why the onclick in the following popup menu gives the error:"Val is undefined"? Does it have something to do with the fact that it is called within the variable tablePop?...
4
by: Chris Beall | last post by:
If you want your code to be bulletproof, do you have to explicitly check for the existence of any possibly-undefined variable? Example: window.outerHeight is defined by some browsers, but not...
49
by: matty | last post by:
Hi, I recently got very confused (well that's my life) about the "undefined" value. I looked in the FAQ and didn't see anything about it. On...
9
by: Brian | last post by:
So I have this: double x = .25; double y = .5; double z = .25; double probability; int index; while(index < 30) {
17
by: yb | last post by:
Hi, Looking for clarification of undefined variables vs. error in JavaScript code. e.g. <script> alert( z ); // this will be an error, i.e. an exception </script>
45
by: VK | last post by:
(see the post by ASM in the original thread; can be seen at <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b> as an option) As that is not in relevance to...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...

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.