undefined value when accessing an element by its name

In the following function I try to validate that an appropriate select value is chosen by a user. The alert popup box displays 'undefined' when I run the script. I need to get the element using its name rather than ID.

Sort table in =
<select name="SortIn">
<option value="Please Choose">Please Choose</option>
<option value="asc">Ascending order</option>
<option value="desc">Descending order</option>
</select>

<input type="submit" name="submit" value="Submit" onclick="return validateForm()"/>
</form>

<script type="text/javascript">
/* <![CDATA [ */

function validateForm()
{
    //Validates that form elements are not empty
            var test = document.getElementsByName('SortIn').value;
    alert(test1);
    if(test1 == null || test == "")
    {
        alert("Please select sorting order");
        return false;
    }
    else
        {
            return true;
        }
}

/* ]]> */
</script>

Answers


document.getElementsByName('SortIn') returns a nodelist. You need to get the first element of that nodelist like so:

var test = document.getElementsByName('SortIn')[0].value;

Need Your Help

What type of struct/container would you use in this instance?

c# data-structures

I am trying to figure out what type of structure or container I should use for a quick project. I need to have an unknown number of sets that will be entered from the GUI (each one will have a name,

How do I attatch a Key value pair to a UIView on iPhone?

cocoa-touch dictionary key-value-coding

When I started iPhone development, I read somewhere that it's possible to attach a key value pair to a UIView. I understood that all UIViews could be used as dictionaries to store any data you may ...