NAME

HTML::Object::DOM::Element::Input - HTML Object DOM Input Class

SYNOPSIS

use HTML::Object::DOM::Element::Input;
my $input = HTML::Object::DOM::Element::Input->new || 
    die( HTML::Object::DOM::Element::Input->error, "\n" );

VERSION

v0.2.0

DESCRIPTION

This interface provides special properties and methods for manipulating the options, layout, and presentation of <input> elements.

INHERITANCE

+-----------------------+     +---------------------------+     +-------------------------+     +----------------------------+     +-----------------------------------+
| HTML::Object::Element | --> | HTML::Object::EventTarget | --> | HTML::Object::DOM::Node | --> | HTML::Object::DOM::Element | --> | HTML::Object::DOM::Element::Input |
+-----------------------+     +---------------------------+     +-------------------------+     +----------------------------+     +-----------------------------------+

PROPERTIES

Inherits properties from its parent HTML::Object::DOM::Element and the properties exported by HTML::Object::DOM::Element::Shared

accept

This returns or sets the element's accept HTML attribute, containing comma-separated list of file types accepted by the server when type is file.

See also Mozilla documentation

accessKey

This returns a string containing a single character that switches input focus to the control when pressed.

Example:

<button accesskey="s">Some button</button>

See also Mozilla documentation, accessKey attribute documentation

allowdirs

This sets or gets a boolean value. This is part of the non-standard Directory Upload API; indicates whether or not to allow directories and files both to be selected in the file list. Implemented only in Firefox and is hidden behind a preference.

See also Mozilla documentation

alt

This returns or sets the element's alt attribute, containing alternative text to use when type is image.

See also Mozilla documentation

autocapitalize

This defines the capitalization behavior for user input. Valid values are none, off, characters, words, or sentences.

See also Mozilla documentation

autocomplete

This returns or sets the element's autocomplete attribute, indicating whether the value of the control can be automatically completed by the browser. Ignored if the value of the type attribute is hidden, checkbox, radio, file, or a button type (button, submit, reset, image). Possible values are:

on

The browser can autocomplete the value using previously stored value

off

The user must explicitly enter a value

See also Mozilla documentation

autofocus

This returns or sets the element's autofocus attribute, which specifies that a form control should have input focus when the page loads, unless the user overrides it, for example by typing in a different control. Only one form element in a document can have the autofocus attribute. It cannot be applied if the type attribute is set to hidden (that is, you cannot automatically set focus to a hidden control).

See also Mozilla documentation

checked

This returns a boolean value, which represents the current state of the element when type is checkbox or radio.

See also Mozilla documentation

defaultChecked

This returns or sets a boolean value, which represents the default state of a radio button or checkbox as originally specified in HTML that created this object.

See also Mozilla documentation

defaultValue

This returns or sets the default value as originally specified in the HTML that created this object.

See also Mozilla documentation

dirName

This returns or sets a string, which represents the directionality of the element.

See also Mozilla documentation

disabled

This returns or sets a boolean value, which represents the element's disabled attribute, indicating that the control is not available for interaction. The input values will not be submitted with the form. See also readonly.

See also Mozilla documentation

files

This returns or accepts a FileList object, which contains a list of File objects representing the files selected for upload. However, this being a perl framework, you would have to set the object values yourself.

See also Mozilla documentation

form

Read-only.

HTML::Object::DOM::Element::Form object: this returns a reference to the parent <form> element.

See also Mozilla documentation

formAction

This returns or sets the element's formaction attribute, containing the URI of a program that processes information submitted by the element. This overrides the action attribute of the parent form.

See also Mozilla documentation

formEnctype

This returns or sets the element's formenctype attribute, containing the type of content that is used to submit the form to the server. This overrides the enctype attribute of the parent form.

See also Mozilla documentation

formMethod

This returns or Sets the element's formmethod attribute, containing the HTTP method that the browser uses to submit the form. This overrides the method attribute of the parent form.

See also Mozilla documentation

formNoValidate

This returns or sets a boolean value, which represents the element's formnovalidate HTML attribute, indicating that the form is not to be validated when it is submitted. This overrides the novalidate attribute of the parent form.

See also Mozilla documentation

formTarget

This returns or sets the element's formtarget attribute, containing a name or keyword indicating where to display the response that is received after submitting the form. This overrides the target attribute of the parent form.

See also Mozilla documentation

height

This returns or sets the element's height attribute, which defines the height of the image displayed for the button, if the value of type is image.

See also Mozilla documentation

indeterminate

This returns a boolean value, which represents whether the checkbox or radio button is in indeterminate state. For checkboxes, the effect is that the appearance of the checkbox is obscured/greyed in some way as to indicate its state is indeterminate (not checked but not unchecked). Does not affect the value of the checked attribute, and clicking the checkbox will set the value to false.

See also Mozilla documentation

inputmode

This provides a hint to browsers as to the type of virtual keyboard configuration to use when editing this element or its contents.

See also Mozilla documentation

labels

Read-only.

This returns an array object of label elements that are labels for this element.

Example:

<label id="label1" for="test">Label 1</label>
<input id="test" />
<label id="label2" for="test">Label 2</label>

window->addEventListener( DOMContentLoaded => sub
{
    my $input = $doc->getElementById( 'test' );
    for( my $i = 0; $i < $input->labels->length; $i++ )
    {
        say( $input->labels->[$i]->textContent ); # "Label 1" and "Label 2"
    }
});

See also Mozilla documentation

list

Read-only.

This returns the element pointed by the list attribute. The property may be undef if no HTML element found in the same tree.

See also Mozilla documentation

max

This returns or sets a string, which represents the element's max attribute, containing the maximum (numeric or date-time) value for this item, which must not be less than its minimum (min attribute) value.

See also Mozilla documentation

maxLength

This returns or sets a long, which represents the element's maxlength attribute, containing the maximum number of characters (in Unicode code points) that the value can have. (If you set this to a negative number, an exception will be thrown.)

See also Mozilla documentation

min

This returns or sets a string, which represents the element's min attribute, containing the minimum (numeric or date-time) value for this item, which must not be greater than its maximum (max attribute) value.

See also Mozilla documentation

minLength

This returns or sets a long, which represents the element's minlength attribute, containing the minimum number of characters (in Unicode code points) that the value can have. (If you set this to a negative number, an exception will be thrown.)

See also Mozilla documentation

multiple

This returns or sets a boolean value, which represents the element's multiple attribute, indicating whether more than one value is possible (e.g., multiple files).

Example:

# $fileInput is a <input type=$file multiple>
my $fileInput = $doc->getElementById('myfileinput');

# If true
if( $fileInput->multiple )
{
    for( my $i = 0; $i < $fileInput->files->length; $i++ )
    {
        # Loop $fileInput->files
    }
}
# Only one $file available
else
{
    my $file = $fileInput->files->item(0);
}

See also Mozilla documentation

name

This returns or sets a string, which represents the element's name attribute, containing a name that identifies the element when submitting the form.

See also Mozilla documentation

pattern

This returns or sets a string, which represents the element's pattern attribute, containing a regular expression that the control's value is checked against. Use the title attribute to describe the pattern to help the user. This attribute applies when the value of the type attribute is text, search, tel, url or email; otherwise it is ignored.

See also Mozilla documentation

placeholder

This returns or sets a string, which represents the element's placeholder attribute, containing a hint to the user of what can be entered in the control. The placeholder text must not contain carriage returns or line-feeds. This attribute applies when the value of the type attribute is text, search, tel, url or email; otherwise it is ignored.

See also Mozilla documentation

readOnly

This returns or sets a boolean value, which represents the element's readonly HTML attribute, indicating that the user cannot modify the value of the control.This is ignored if the value of the type attribute is hidden, range, color, checkbox, radio, file, or a button type.

See also Mozilla documentation

required

This returns or sets a boolean value, which represents the element's required attribute, indicating that the user must fill in a value before submitting a form.

See also Mozilla documentation

selectionDirection

This returns or sets a string, which represents the direction in which selection occurred. Possible values are:forward if selection was performed in the start-to-end direction of the current localebackward for the opposite directionnone if the direction is unknown

See also Mozilla documentation

selectionEnd

This returns or sets an unsigned long, which represents the end index of the selected text. When there's no selection, this returns the offset of the character immediately following the current text input cursor position.

See also Mozilla documentation

selectionStart

This returns or sets an unsigned long, which represents the beginning index of the selected text. When nothing is selected, this returns the position of the text input cursor (caret) inside of the input element.

See also Mozilla documentation

size

This returns or sets an unsigned long, which represents the element's size attribute, containing visual size of the control. This value is in pixels unless the value of type is text or password, in which case, it is an integer number of characters. Applies only when type is set to text, search, tel, url, email, or password; otherwise it is ignored.

See also Mozilla documentation

src

This returns or sets a string, which represents the element's src attribute, which specifies a URI for the location of an image to display on the graphical submit button, if the value of type is image; otherwise it is ignored.

See also Mozilla documentation

step

This returns or sets a string, which represents the element's step attribute, which works with min and max to limit the increments at which a numeric or date-time value can be set. It can be the string any or a positive floating point number. If this is not set to any, the control accepts only values at multiples of the step value greater than the minimum.

See also Mozilla documentation, documentation on step attribute

type

This returns or sets a string, which represents the element's type attribute, indicating the type of control to display. See type attribute of <input> for possible values.

See also Mozilla documentation

validationMessage

Read-only.

This returns or sets a string, which represents a localised message that describes the validation constraints that the control does not satisfy (if any). This is the empty string if the control is not a candidate for constraint validation (willvalidate is false), or it satisfies its constraints. This value can also be set by the "setCustomValidity" method.

See also Mozilla documentation

validity

Read-only.

This returns or sets the element's current validity state object.

See also Mozilla documentation

value

This returns or sets a string, which represents the current value of the control.

Note: If the user enters a value different from the value expected, this may return an empty string.

This interface does not enforce proper value, so it is up to you to use the right one. For example, using a value of 2021-W55 for an input of type week is normally illegal since the week number value should be a number between 1 and 53.

See also Mozilla documentation

valueAsDate

This returns or sets a date, which represents the value of the element, interpreted as a date, or undef if conversion is not possible.

See also Mozilla documentation

valueAsNumber

This returns a double, which represents the value of the element, interpreted as one of the following, in order:

A time value
A number
NaN (undef) if conversion is impossible

See also Mozilla documentation

webkitEntries

This always returns undef under perl.

Under JavaScript, this returns an Array of FileSystemEntry objects that describes the currently selected files or directories.

See also Mozilla documentation

webkitdirectory

This returns or sets a boolean value, which represents the webkitdirectory HTML attribute; if true, the file system picker interface only accepts directories instead of files.

Example:

<input type="file" id="filepicker" name="fileList" webkitdirectory multiple />
<ul id="listing"></ul>

$doc->getElementById( 'filepicker' )->addEventListener( change => sub
{
    my $output = $doc->getElementById( 'listing' );
    my $files = event->target->files;

    for( my $i=0; $i < $files->length; $i++ )
    {
        my $item = $doc->createElement( 'li' );
        $item->innerHTML = $files->[$i]->webkitRelativePath;
        $output->appendChild( $item );
    };
}, { capture => 0 });

See also Mozilla documentation

width

This returns or sets a string, which represents the element's width attribute, which defines the width of the image displayed for the button, if the value of type is image.

See also Mozilla documentation

willValidate

Read-only.

This returns or sets a boolean value, which represents whether the element is a candidate for constraint validation. It is false if any conditions bar it from constraint validation, including: its type is hidden, reset, or button; it has a datalist ancestor; its disabled property is true.

See also Mozilla documentation

METHODS

Inherits methods from its parent HTML::Object::DOM::Element

mozGetFileNameArray

Since this is a perl environment, this has no effect and always returns undef.

See also Mozilla documentation

mozSetFileArray

Since this is a perl environment, this has no effect and always returns undef.

See also Mozilla documentation

setCustomValidity

Sets a custom message to display if the input element's value is not valid.

See Mozilla documentation

stepDown

Decrements the value by (step * n), where n defaults to 1 if not specified. Returns an HTML::Object::InvalidStateError error:

  • if the method is not applicable to for the current type value,

  • if the value cannot be converted to a number or recognised as a DateTime object when applicable,

  • if the resulting value is above the "max" or below the "min" set by their respective HTML attribute.

If the element has no step value, step defaults to 1.

Supported types and equivalent values are:

  • date

    Unit is 1 (day).

    <input type="date" min="2019-12-25" step="1" />

    This defaults to 1970-01-01 if not value is set yet, but if a max value is set and "stepDown" is called, the initial value will be that of max and if min value is set and "stepUp" is called, the initial value will be that of min

  • month

    Unit is 1 (month).

    <input type="month" min="2019-12" step="12" />

    This defaults to 1970-01 if not value is set yet, but if a max value is set and "stepDown" is called, the initial value will be that of max and if min value is set and "stepUp" is called, the initial value will be that of min

  • week

    Unit is 1 (week).

    <input type="week" min="2019-W23" step="2" />

    This defaults to 1970-W1 if not value is set yet, but if a max value is set and "stepDown" is called, the initial value will be that of max and if min value is set and "stepUp" is called, the initial value will be that of min

  • time

    Unit is 60 (seconds).

    <input type="time" min="09:00" step="900" />

    This defaults to 00:00 if not value is set yet, but if a max value is set and "stepDown" is called, the initial value will be that of max and if min value is set and "stepUp" is called, the initial value will be that of min

  • datetime-local

    Unit is 1 (second).

    <input type="datetime-local" min="2019-12-25T19:30:01" step="7" />

    This defaults to 1970-01-01T00:00:00 if not value is set yet, but if a max value is set and "stepDown" is called, the initial value will be that of max and if min value is set and "stepUp" is called, the initial value will be that of min

    This also supports the following format 2019-12-25T19:30, i.e. without seconds, and 2019-12-25T19, i.e. without minutes or seconds

  • number

    Unit is 1.

    <input type="number" min="0" step="0.1" max="10" />
  • range

    Unit is 1.

    <input type="range" min="0" step="2" max="10" />

Interestingly enough, if you have the following HTML:

<input type="time" min="17:00" step="900" />

and call stepUp, it will set the value the first time to 17:00, then the second time to 17:15.

However, the other way around is not true, i.e.

<input type="time" max="17:00" step="900" />

Then a call to stepDown will not yield the value 17:00, but instead 23:45, the second time to 17:00 and third time to 16:45. A bug report No 1749427 was filed to Mozilla and to Chromium on 2022-01-11 and is preemptively corrected here in this interface.

Example:

<!--    decrements by intervals of 900 seconds (15 minute) -->
<input type="time" max="17:00" step="900" />

<!-- decrements by intervals of 7 days (one week) -->
<input type="date" max="2019-12-25" step="7" />

<!-- decrements by intervals of 12 months (one year) -->
<input type="month" max="2019-12" step="12" />

$element->stepDown( [ $stepDecrement ] );

Another example:

<p>
    <label>Enter a number between 0 and 400 that is divisible by 5:
        <input type="number" step="5" id="theNumber" min="0" max="400" />
    </label>
</p>
<p>
    <label>Enter how many values of step you would like to decrement by or leave it blank:
        <input type="number" step="1" id="decrementer" min="-2" max="15" />
    </label>
</p>
<input type="button" value="Decrement" id="theButton" />

# make the $button call the function
my $button = $doc->getElementById('theButton');
$button->addEventListener( click => sub
{
    &stepondown();
});

sub stepondown
{
    my $input = $doc->getElementById('theNumber');
    my $val = $doc->getElementById('decrementer')->value;

    # decrement with a parameter
    if( $val )
    {
        $input->stepDown( $val );
    }
    # or without a parameter. Try it with 0, 5, -2, etc.
    else
    {
        $input->stepDown();
    }
}

See also Mozilla documentation, and Mozilla documentation on step

stepUp

Increments the value by (step * n), where n defaults to 1 if not specified. Returns an HTML::Object::InvalidStateError error:

  • if the method is not applicable to for the current type value.,

  • if the element has no step value,

  • if the value cannot be converted to a number,

  • if the resulting value is above the max or below the min.

Supported types and equivalent values are the same as for "stepDown":

Example:

<p>
    <label>Enter a number between 0 and 400 that is divisible by 5:
        <input type="number" step="5" id="theNumber" min="0" max="400" />
    </label>
</p>
<p>
    <label>Enter how many values of step you would like to increment by or leave it blank:
        <input type="number" step="1" id="incrementer" min="0" max="25" />
    </label>
</p>
<input type="button" value="Increment" id="theButton" />

# make the $button call the function
my $button = $doc->getElementById('theButton')
$button->addEventListener( click => sub
{
    &steponup();
})

sub steponup
{
    my $input = $doc->getElementById('theNumber');
    my $val = $doc->getElementById('incrementer')->value;
    # increment with a parameter
    if( $val )
    {
        $input->stepUp( $val );
    }
    # or without a parameter. Try it with 0
    else
    {
        $input->stepUp();
    }
}

Another example:

<input max="4096" min="1" name="size" step="2" type="number" value="4096" id="foo" />

See also Mozilla documentation, and Mozilla documentation on step

EVENTS

Event listeners for those events can also be found by prepending on before the event type:

For example, input event listeners can be set also with oninput method:

$e->oninput(sub{ # do something });
# or as an lvalue method
$e->oninput = sub{ # do something };

input

Fires when the value of an input, select, or textarea element has been changed. Note that this is actually fired on the HTML::Object::DOM::Element interface and also applies to contenteditable elements, but we've listed it here because it is most commonly used with form input elements. Also available via the oninput event handler property.

Example:

<input placeholder="Enter some text" name="name" />
<p id="values"></p>

my $input = $doc->querySelector('input');
my $log = $doc->getElementById('values');

$input->addEventListener( input => \&updateValue );

sub updateValue
{
    my $e = shift( @_ );
    $log->textContent = $e->target->value;
}

See also Mozilla documentation

invalid

Fired when an element does not satisfy its constraints during constraint validation. Also available via the oninvalid event handler property.

Example:

<form action="#">
    <ul>
        <li><label>Enter an integer between 1 and 10: <input type="number" min="1" max="10" required></label></li>
        <li><input type="submit" value="submit"></li>
    </ul>
</form>
<p id="log"></p>

my $input = $doc->querySelector('input');
my $log = $doc->getElementById('log');

$input->addEventListener( invalid => \&logValue );
sub logValue
{
    my $e = shift( @_ )l
    $log->textContent = $e->target->value;
}

See also Mozilla documentation

Fired when a search is initiated on an <input> of type="search". Also available via the onsearch event handler property.

Example:

# addEventListener version
my $input = $doc->querySelector('input[type="search"]');

$input->addEventListener( search => sub
{
    say( "The term searched for was " . $input->value );
})

# onsearch version
my $input = $doc->querySelector( 'input[type="search"]' );

$input->onsearch = sub
{
    say( "The term searched for was " + $input->value );
})

See also Mozilla documentation

selectionchange

Fires when the text selection in a input element has been changed.

Example:

<div>Enter and select text here:<br />
    <input id="mytext" rows="2" cols="20" />
/div>
<div>selectionStart: <span id="start"></span></div>
<div>selectionEnd: <span id="end"></span></div>
<div>selectionDirection: <span id="direction"></span></div>

my $myinput = $doc->getElementById( 'mytext' );

$myinput->addEventListener( selectionchange => sub
{
    $doc->getElementById( 'start' )->textContent = $myinput->selectionStart;
    $doc->getElementById( 'end' )->textContent = $myinput->selectionEnd;
    $doc->getElementById( 'direction' )->textContent = $myinput->selectionDirection;
});

See also Mozilla documentation

DEPRECATED PROPERTIES

align

Provided with a string, and this sets or gets the HTML attribute that represents the alignment of the element. It is better to use CSS instead.

See also Mozilla documentation

useMap

A string reflecting the usemap HTML attribute, containing the page-local URL of the <map> element describing the image map to use. The page-local URL is a pound (hash) symbol (#) followed by the ID of the <map> element, such as #my-map-element. The <map> in turn contains <area> elements indicating the clickable areas in the image.

Example:

<map name="mainmenu-map">
    <area shape="circle" coords="25, 25, 75" href="/index.html" alt="Return to home page">
    <area shape="rect" coords="25, 25, 100, 150" href="/index.html" alt="Shop">
</map>

<input usemap="#mainmenu-map" />

See also Mozilla documentation

AUTHOR

Jacques Deguest <jack@deguest.jp>

SEE ALSO

Mozilla documentation, Mozilla documentation on input element

COPYRIGHT & LICENSE

Copyright(c) 2021 DEGUEST Pte. Ltd.

All rights reserved

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.