Referring to form select fields.

R

RogerF

Hello I'm trying to refer to a select field in a form, but am having a
problem.
Here is the snipet of code:
...
...
html += '<td class="plaintablerowheader" style="width:40%">'+"Tax
Forms " +'</td>'
html += '<td id=w4list class="plaintablecell">'
html += '<select class="inputbox" name=w4list>'
html += BuildAllW4Select()
html += '</select>'
html += '<a href="" onClick="window.open(\''
html += GetAllW4StForm(this.form)
html += '\')'
html += ';return false;">'
html += '<a href="" onClick="window.open(\''
html += GetAllW4StForm(this.form)
html += '\')'
html += ';return false;">'

Then I have the function defined later on:
function GetAllW4StForm(formobj)
{
var tempurl = ""
var statew4url = ""
var stlinkfound = false

var f = document.getElementById(formobj);
alert(f);
for (var i=0; i<StMarStatusList.length && stlinkfound == false; i++)
{
if (stlinkfound == false && formobj.w4list.selectedIndex == i)
...
...


Well the problem when I run this is it's erroring out on the
formobj.w4list.selectIndex. The javascript message from the browser
reads:

'formobj.w4list.selectedIndex' is null or not an object.

Also the alert(f) returns/displays the value "null".

How do I refer in the function to the w4list select field? I've tried
a coulple of different ways but they also don't work.

Thanks in advance,

Roger
 
T

Thomas 'PointedEars' Lahn

RogerF said:
Hello I'm trying to refer to a select field in a form, but am having a
problem.
Here is the snipet of code:
...
...
html += '<td class="plaintablerowheader" style="width:40%">'+"Tax
Forms " +'</td>'
html += '<td id=w4list class="plaintablecell">'
html += '<select class="inputbox" name=w4list>'
html += BuildAllW4Select()
html += '</select>'
html += '<a href="" onClick="window.open(\''
html += GetAllW4StForm(this.form)
[aso.]

Then I have the function defined later on:
function GetAllW4StForm(formobj)
{
var tempurl = ""
var statew4url = ""
var stlinkfound = false

var f = document.getElementById(formobj);
alert(f);
for (var i=0; i<StMarStatusList.length && stlinkfound == false; i++)
{
if (stlinkfound == false && formobj.w4list.selectedIndex == i)
...
[...]
Well the problem when I run this is it's erroring out on the
formobj.w4list.selectIndex. The javascript message from the browser
reads:

'formobj.w4list.selectedIndex' is null or not an object.

Also the alert(f) returns/displays the value "null".

Given the absence of evidence to the contrary, we must assume that the code
that concatenates the `html' markup string (in an utterly inefficient
manner, consider `+' instead) runs in the global execution context.

In that case, `this' refers to the global object. The global object does
not have a `form' property, so the property access `this.form' results in
`undefined'.

`undefined' passed to GetAllW4StForm() results in the local `formobj' having
the value `undefined'. `undefined' passed to document.getElementById()
results in `null' (probably as a result of `undefined' being type-converted
to "undefined" and having found no element with that ID), which is what the
alert message box (which should be created with window.alert(...)) shows.

The value of `f', which is `null', has no bearing on the rest of the code,
so the `for' statement is executed. As `StMarStatusList.length' supposedly
results in a value greater than 0, and we must assume from your description
that `stlinkfound == false', the condition is met and the following block
statement is executed.

In the block statement there is the following `if' statement:

if (stlinkfound == false && formobj.w4list.selectedIndex == i)

Per our assumption the first operand of the `&&' operation is already true,
so the second operand is evaluated. As `formobj' is `undefined', the
property access is equivalent to

undefined.w4list.selectedIndex

However, `undefined' is a primitive value (the sole value of the Undefined
type), and therefore does not have any properties, especially not one named
`w4list'. As a result, a TypeError exception is thrown, which is not
catched by your code, and the program stops with an error result.

In order to refer to a `select' element within a form element, use

formobj.elements["foo"]

where `formobj' is the form object reference, and `foo' is the value of
the `select' element's `name' attribute (newer and W3C DOM-compliant
implementations also support `id' attribute values there).

Note that there may be more than one element with the same name (value of
the `name' attribute) which would create a NodeList of matching elements in
the DOM tree for formobj.elements["foo"]; so you might need
formobj.elements["foo"][n] where n is the zero-based index of the element
in that NodeList.

Please use a debugger next time, e.g. <http://getfirebug.com/>
See also <http://jibbering.com/faq/#posting>.


PointedEars
 
R

RogerF

Hello Thomas,
Thank you very much for your very thorough explanation. Really
appreciated.

I tried the formobj.elements["w4list"] and it came back as null or not
an object.
I also tried various combinations.
document.mainform.w4list.selectedIndex also came back as null.

I am a little stumped.

Thanks,
Roger


RogerF said:
Hello I'm trying to refer to a select field in a form, but am having a
problem.
Here is the snipet of code:
                ...
                ...
   html += '<td class="plaintablerowheader" style="width:40%">'+"Tax
Forms " +'</td>'
   html += '<td id=w4list class="plaintablecell">'
   html += '<select class="inputbox" name=w4list>'
           html += BuildAllW4Select()
           html += '</select>'
   html += '<a href="" onClick="window.open(\''
   html += GetAllW4StForm(this.form)
[aso.]
Then I have the function defined later on:
function GetAllW4StForm(formobj)
{
   var tempurl        = ""
   var statew4url = ""
   var stlinkfound = false
   var f = document.getElementById(formobj);
   alert(f);
   for (var i=0; i<StMarStatusList.length && stlinkfound == false; i++)
   {
   if (stlinkfound == false && formobj.w4list.selectedIndex == i)
               ...
[...]
Well the problem when I run this is it's erroring out on the
formobj.w4list.selectIndex. The javascript message from the browser
reads:
'formobj.w4list.selectedIndex' is null or not an object.
Also the alert(f) returns/displays the value "null".

Given the absence of evidence to the contrary, we must assume that the code
that concatenates the `html' markup string (in an utterly inefficient
manner, consider `+' instead) runs in the global execution context.

In that case, `this' refers to the global object.  The global object does
not have a `form' property, so the property access `this.form' results in
`undefined'.

`undefined' passed to GetAllW4StForm() results in the local `formobj' having
the value `undefined'.  `undefined' passed to document.getElementById()
results in `null' (probably as a result of `undefined' being type-converted
to "undefined" and having found no element with that ID), which is what the
alert message box (which should be created with window.alert(...)) shows.

The value of `f', which is `null', has no bearing on the rest of the code,
so the `for' statement is executed.  As `StMarStatusList.length' supposedly
results in a value greater than 0, and we must assume from your description
that `stlinkfound == false', the condition is met and the following block
statement is executed.

In the block statement there is the following `if' statement:

  if (stlinkfound == false && formobj.w4list.selectedIndex == i)

Per our assumption the first operand of the `&&' operation is already true,
so the second operand is evaluated.  As `formobj' is `undefined', the
property access is equivalent to

  undefined.w4list.selectedIndex

However, `undefined' is a primitive value (the sole value of the Undefined
type), and therefore does not have any properties, especially not one named
`w4list'.  As a result, a TypeError exception is thrown, which is not
catched by your code, and the program stops with an error result.

In order to refer to a `select' element within a form element, use

  formobj.elements["foo"]

where `formobj' is the form object reference, and `foo' is the value of
the `select' element's `name' attribute (newer and W3C DOM-compliant
implementations also support `id' attribute values there).

Note that there may be more than one element with the same name (value of
the `name' attribute) which would create a NodeList of matching elements in
the DOM tree for formobj.elements["foo"]; so you might need
formobj.elements["foo"][n] where n is the zero-based index of the element
in that NodeList.

Please use a debugger next time, e.g. <http://getfirebug.com/>
See also <http://jibbering.com/faq/#posting>.

PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
)  // Plone, register_function.js:16- Hide quoted text -

- Show quoted text -
 
T

Thomas 'PointedEars' Lahn

RogerF said:
I tried the formobj.elements["w4list"] and it came back as null or not
an object.

Of course that modification alone would not help. You need to change the
value you are passing to the function so that the local `formobj' would be
initialized properly.
I also tried various combinations.
document.mainform.w4list.selectedIndex also came back as null.

I don't see a `mainform' in your code.
I am a little stumped.

Use a debugger ...
[top post]

.... and learn to quote.


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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top