Skip to the content.

$.fn.val([value])

Get the value of the first form element in the collection, or set the value of every form element.

Handles the common form elements:

Signatures

val(): string | string[] | undefined;
val(value: string | number | string[] | ((this: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement, index: number, currentValue: string) => string | number | string[])): this;

Parameters

Returns

When getting, the current value (string, string[] for multi-selects, or undefined if the collection is empty or the element has no value). When setting, the original Dabby collection for chaining.

Examples

import $ from "dabbyjs";
import "dabbyjs/attributes/val/val";

// Read the current value
const email = $("input[name=email]").val();
import $ from "dabbyjs";
import "dabbyjs/attributes/val/val";

// Set a value
$("input[name=q]").val("dabbyjs");
import $ from "dabbyjs";
import "dabbyjs/attributes/val/val";

// Pre-select options in a multi-select
$("select#tags").val(["typescript", "dom", "library"]);
import $ from "dabbyjs";
import "dabbyjs/attributes/val/val";

// Trim every text input
$("input[type=text]").val(function (index, current) {
    return current.trim();
});

See also