Inputfield::attr() method

Get or set an attribute (or multiple attributes)

  • To get an attribute call this method with just the attribute name.
  • To set an attribute call this method with the attribute name and value to set.
  • You can also set multiple attributes at once, see examples below.
  • To get all attributes, just specify boolean true as first argument (since 3.0.16).

Example

// Get the "value" attribute
$value = $inputfield->attr('value');

// Set the "value" attribute
$inputfield->attr('value', 'Foo and Bar');

// Set multiple attributes
$inputfield->attr([
  'name' => 'foobar',
  'value' => 'Foo and Bar',
  'class' => 'foo-bar',
]);

// Set name and id attribute to "foobar"
$inputfield->attr("name+id", "foobar");

// Get all attributes in associative array (since 3.0.16)
$attrs = $inputfield->attr(true); 

Usage

// basic usage
$inputfield = $inputfield->attr($key);

// usage with all arguments
$inputfield = $inputfield->attr($key, $value = null);

Arguments

NameType(s)Description
keystring, array, bool

Specify one of the following:

  • Name of attribute to get (if getting an attribute).
  • Name of attribute to set (if setting an attribute, and also specifying a value).
  • Aassociative array to set multiple attributes.
  • String with attributes split by "+" or "|" to set them all to have the same value.
  • Specify boolean true to get all attributes in an associative array.
value (optional)string, int, bool, null

Value to set (if setting), omit otherwise.

Return value

Inputfield array string int object float

If setting an attribute, it returns this instance. If getting an attribute, the attribute is returned.

See Also


Inputfield methods and properties

API reference based on ProcessWire core version 3.0.244

Latest news

  • ProcessWire Weekly #559
    The 559th issue of ProcessWire Weekly brings in all the latest news from the ProcessWire community. Modules, sites, and more. Read on!
    Weekly.pw / 25 January 2025
  • ProcessWire 3.0.244 new main/master version
    ProcessWire 3.0.244 is our newest main/master/stable version. It's been more than a year in the making and is packed with tons of new features, issue fixes, optimizations and more. This post covers all the details.
    Blog / 18 January 2025
  • Subscribe to weekly ProcessWire news

“ProcessWire is like a breath of fresh air. So powerful yet simple to build with and customise, and web editors love it too.” —Margaret Chatwin, Web developer