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

Beginner elements question

Can someone please explain why getObjects2 works and getObjects1 fails?
Thank you.

function getObjects1 ( obj_name) {
var objs = eval( "document.main_form.elements[obj_name]" );
if ( objs != null && eval( "objs.length" ) == null ) objs = new
Array( objs );
if ( objs == null ) objs = new Array();
return objs;
}
function getObjects2 ( obj_name ) {
var objs = eval( "document.main_form.elements[obj_name]" );
if ( objs != null && eval( "objs.length" ) == null &&
eval("objs.type") == 'single-selection' ) objs = new Array( objs );
if ( objs == null ) objs = new Array();
return objs;
}

Jul 23 '05 #1
3 1321
On 27/05/2005 18:28, so***********@yahoo.com wrote:
function getObjects1 ( obj_name) {
var objs = eval( "document.main_form.elements[obj_name]" );
The use of eval here is totally unnecessary, and in every other location
in your post. The eval function has a very specific purpose: executing
an arbitrary string provided at run-time as program code. You are highly
unlikely to /ever/ need to use this function.

[snip]
eval("objs.type") == 'single-selection'


The type property will never have the value, 'single-selection'. The
expression above should be:

'select-one' == objs.type

[snip]

A more generic approach is:

function getElements(name) {
var elements = document.forms.main_form.elements[name];

if(elements && ('string' == typeof elements.type)) {
elements = [elements];
} else if(!elements) {
elements = [];
}
return elements;
}

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
Thanks for your help. Won't eval return undef if the 'elements' doesn't
exist instead of a javascript error?

Jul 23 '05 #3
On 27/05/2005 20:29, so***********@yahoo.com wrote:
Won't eval return undef if the 'elements' doesn't exist
Which 'elements'? I use that identifier twice in two different ways.
instead of a javascript error?


The eval function won't suppress errors. Both syntax and run-time errors
will result in exceptions.

The only real reason why the code I posted might error out is if there
is no 'main_form' in the document. I don't know why you'd call the
function in this case, but this exceptional condition is detectable as well.

/* group - The name of a group of form controls
* form - The name, index, or id of a form, or
* a reference to a form element
*/
function getElements(group, form) {
/* If the form argument is a string, try
* to find a form with that name or id.
*/
if(('string' == typeof form) || ('number' == typeof form)) {
form = document.forms[form];
}
/* The form variable should now be either
* an object reference or null/undefined.
*/
if(form) {
/* Find all controls within form with
* the name passed via group.
*/
group = form.elements[group];

/* If group is not null, and has a
* string property, type, it is form
* control so insert it into an array.
*
if(group && ('string' == typeof group.type)) {
group = [group];

/* If group is null, no controls were
* found so return an empty array.
*/
} else if(!group) {
group = [];
}
/* In the remaining case (not null and
* no type property), group is a
* collection so return as-is.
*/
} else {
group = [];
}
return group;
}

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #4

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

Similar topics

4
by: Shailesh | last post by:
Hi! I want to overload << operator so that it can print an arry defined in MyClass.My problem is that I want to print only a no of elements NOT all the elements in the array and this no of...
4
by: Pekka Karjalainen | last post by:
Hi, this is my first posting here. I also have a question about etiquette. There's a html file associated with my question. I cannot host it indefinitely at the current location. I don't, however,...
5
by: Tarjei Romtveit | last post by:
I'm still a newbie into C++ programming, so I got a quite foolish string related question. Using: Dev-cpp 4.9.9.2 (I think Dev-Cpp uses a gcc compiler of some sort) If i declare a char...
4
by: Michael | last post by:
Thanks to everyone who responded on my last question. I have come across something I don't understand, I had a good look through the FAQ first, and I didn't find anything about it. I have an...
11
by: rory | last post by:
Cna anyone point me in the right direction, I have a struture in my .h file: typedef struct mdata { int names; int dates; int ages; }m_Data;
4
by: Adrian von Bidder | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Yo! Mostly as a finger-exercise (and because I'm annoyed again and again how bad the existing solutions are), I'm hacking up a web-based forum...
5
by: macca | last post by:
Hi, I'm looking for a good book on PHP design patterns for a OOP beginner - Reccommendations please? Thanks Paul
12
by: nodrogbrown | last post by:
hi i want to create an array of strings .I know the number of items that the array will contain but the size of each string will be different and will not be known at compile time. eg: in char...
3
by: mick | last post by:
hello, I need to learn enough xml to use a piece of software. can anybody point me to a friendly guide. right now I want to know exactly what a placeholder is. I kind of understand but not really...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.