up.form up.form.group(element, [options])
JavaScript function

This feature is experimental. Please share your experiences so we know what to keep or change.

Returns the form group for the given element.

By default, a form group is a container with an [up-form-group] attribute, a <fieldset> or a <label>. This can be configured in up.form.config.groupSelectors.

Form groups may be nested. This function returns the closest group around the given element. If no closer group is found, the <form> element is returned.

Example

This is a form with two groups:

<form>
  <fieldset>
    <label for="email">E-mail</label>
    <input type="text" name="email" id="email">
  </fieldset>
  <fieldset>
    <label for="password">Password</label>
    <input type="text" name="password" id="password">
  </fieldset>
</form>

We can now retrieve the form group for any element in the form:

let passwordField = document.querySelector('#password')
let group = up.form.group(passwordField) // returns second <fieldset>

Parameters

element
required

The element for which to find a form group.

Type: Element
[options.targetable=false]
optional

Only return a group that Unpoly can target with a selector.

Type: boolean

Return value

The closest form group around the given element.

If no better group can be found, the <form> element is returned.

If the element is not within a <form>, returns undefined.

With { targetable: true }, returns undefined when no group around the element can be targeted.

Type: Element | undefined