phpQueryObject::attr PHP Method

attr() public method

public attr ( $attr = null, $value = null )
    public function attr($attr = null, $value = null)
    {
        foreach ($this->stack(1) as $node) {
            if (!is_null($value)) {
                $loop = $attr == '*' ? $this->getNodeAttrs($node) : array($attr);
                foreach ($loop as $a) {
                    $oldValue = $node->getAttribute($a);
                    $oldAttr = $node->hasAttribute($a);
                    // TODO raises an error when charset other than UTF-8
                    // while document's charset is also not UTF-8
                    @$node->setAttribute($a, $value);
                    $this->attrEvents($a, $oldAttr, $oldValue, $node);
                }
            } elseif ($attr == '*') {
                // jQuery difference
                $return = array();
                foreach ($node->attributes as $n => $v) {
                    $return[$n] = $v->value;
                }
                return $return;
            } else {
                return $node->hasAttribute($attr) ? $node->getAttribute($attr) : null;
            }
        }
        return is_null($value) ? '' : $this;
    }

Usage Example

 private function addTextarea(\phpQueryObject $input, &$data, $value = null)
 {
     $data[$input->attr('name')] = "Any text";
 }