How to tell if a dropdown is defined on page

G

GIMME

How do you tell if a drop down has been placed on a page?

In this instance I have a form named aForm and I'm checking
for a dropdown named div_list .


var div_list document.aForm.div_list.options;
if (!div_list) {}

fails as does

if ( null == typeof(document.aForm.div_list.options )


Thanks.
 
J

Janwillem Borleffs

GIMME said:
How do you tell if a drop down has been placed on a page?

In this instance I have a form named aForm and I'm checking
for a dropdown named div_list .

Use the `type` property:
document.aForm.div_list.type

It's value is either select-one or select-multiple (the latter for a
multiple select object).

When you want to know the number of defined options, use:
document.aForm.div_list.options.length


JW
 
L

Lasse Reichstein Nielsen

How do you tell if a drop down has been placed on a page?

I assume you mean a select element. There are many ways,
depending on what you know about it.
In this instance I have a form named aForm and I'm checking
for a dropdown named div_list .

Much simpler, you know the name of the form and the select.
var div_list document.aForm.div_list.options;

Use
var div_lust = document.formes['aForm'].elements['div_list'];
This will be undefined if the element with name div_list isn't there.
Trying to get the options property will give an error if the select
element isn't there, so only do that after you have tested that it
exists.
if (!div_list) {}

div_list = div_list.options;
fails as does

if ( null == typeof(document.aForm.div_list.options )

Yes, you still try to find the options property of the undefined value.

/L
 
T

Thomas 'PointedEars' Lahn

Janwillem said:
Use the `type` property:
document.aForm.div_list.type

It's value is either select-one or select-multiple (the latter for a
multiple select object).

When you want to know the number of defined options, use:
document.aForm.div_list.options.length

In fact he wanted to know if there is a "select" element in the
document. Your code can be useful but it will fail if there is
no "document", "document.aForm" or "document.aForm.div_list".
So as Lasse wrote, you first need to test if the parents of the
object and the object itself exist before you can check its "type"
property. And even then you may trigger a script error as there
may be no "type" property or reading it is forbidden (e.g. due to
missing ECMAScript conformance of the UA's script engine). You can
(and should) test for the former, however you can't test for the
latter because the test itself triggers the error and you cannot
catch it. Same goes for options.length.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top