checking if variable exists code

Discussion in 'Javascript' started by wg, Oct 24, 2005.

  1. wg

    wg Guest


    Can someone show my how to change the code below to check to see if a
    variable exists rather than if the variable contains an empty string.

    if (document.form.regionselection.value == "") {
    missinginfo += "\n - Region";

    wg, Oct 24, 2005
    1. Advertisements

  2. wg

    Ivo Guest

    This code checks a form element, not a genuine variable as such. To check if
    a form element exists, you simply test if its name occurs in the form
    elements collection, like so:

    if ( document.form.regionselection ) {
    // element exists, now check its value

    or even betterr:

    if ( document.forms.form.elements.regionselection ) {

    This is assuming your form has id="form" (I'd chose a less confusing name

    Ivo, Oct 24, 2005
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.