JavaScript

$n Function

Syntax

$n(eleName as string [..., eleName-N as string])

Arguments

eleNamestring

The name of the element(s) to find.

eleName-nstring

(optional) One or more additional names of the element(s) to find. Separate each name with a comma.

Description

Get a pointer to elements in the DOM by name.

The $n() function gets elements in the HTML. It takes an arbitrary number of arguments, each a string with the eleName of the element you wish to get a pointer to.

Getting elements by name returns an array because the name attribute is used to describe groups of controls - such as radio and checkbox groups. Therefore, even if there is only a single element with the given eleName, it will be returned inside a Javascript array.

If you pass a single eleName, the function will return a Javascript array of pointers to the elements that match the eleName passed in.

If you pass it multiple eleNames, the function will return a Javascript array of arrays of the HTML elements.

If an element with the eleName specified does not exist, the function will automatically look for an element that has a ID of the passed in string, and return it.

Example

/*Assume that you have two radio buttons with choices 'Male' and 'Female'. Each radio button has a unique ID, but they both have the same NAME, 'gender' */
var genderInputs = $n('gender');

/*Select the first radio button in the group*/
genderInputs[0].checked = true;

See Also