[thelist] simple but baffling JS problem...

jeff jeff at members.evolt.org
Mon Feb 5 01:07:37 CST 2001


john,

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: From: John Corry
:
: function updateValue(propName)
: {
:   var hidey = 'parent.topFrame.document.form1';
:   hidey.propName.value="true";
: }
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

without getting into the rest of your problem, i'm noticing two things with
your function above that are causing problems.

first, you're setting the value of "hidey" as a string (because the value is
wrapped in single-quotes) and not an object reference.  that's part of the
reason it's throwing an error.  instead, remove the single-quotes from
around the value and it'll become an object reference.

var hidey = parent.topFrame.document.form1;

second, since you're passing in a string via the "propName" argument of the
function, you can't reference it as an object of the "hidey" variable.  it
will fail, as you're experiencing.  so, since it's just a string
representing the form field's name, access it via the elements array, like
so:

hidey.elements[propName].value = 'true';

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: <input
:  type="checkbox"
:  name="sugarcove"
:  onclick="updateValue(self.name);">
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

another reason you may be having problems is the method you're using to
access the form fields properties, ie "name".  the keyword "self" is used to
refer to the window object, not the object the reference is contained in.
the keyword you should be using is "this".  so, the onClick event handler
above should look like so:

updateValue(this.name)

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: Naturally, the other form (which is just
: a series of inputs designed to do nothing
: more than store the values true pr false
: depending on which checkboxes are selected)
: is in the other frame.
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

i'm curious why you're storing them in form fields, especially if the frame
is completely hidden.  seems to me it would be so much easier to simply
store them in an associative array where the key for any element is the name
of the form field that's checked.  using the checkbox name above, it'd look
like this:

var vacProps = new Array();
    vacProps['sugarcove'] = false;

then, your updateValue() function would look like this:

function updateValue(propName)
{
  parent.topFrame.vacProp[propName] = true;
}

then, to retrieve all the properties they've selected, simply loop over the
vacProp array with a for/in loop, executing on those elements in the array
that are true.

for(i in vacProp)
{
  if(vacProp[i]) // if the value of this element is true
  {
    // do your stuff here if the value is true;
  }
}

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: When you click the checkbox, an error is
: generated that says 'propName is null or
: not an object'. But, as far as I can tell
: ...it IS TOO an object!
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

nope, it's complaining about it not being an object cause it's not - you now
know why.

good luck,

.jeff

name://jeff.howden
game://web.development
http://www.evolt.org/
mailto:jeff at members.evolt.org





More information about the thelist mailing list