phpQueryObject::val PHP Method

val() public method

Return form element value.
public val ( $val = null ) : string
return string Fields value.
    public function val($val = null)
    {
        if (!isset($val)) {
            if ($this->eq(0)->is('select')) {
                $selected = $this->eq(0)->find('option[selected=selected]');
                if ($selected->is('[value]')) {
                    return $selected->attr('value');
                } else {
                    return $selected->text();
                }
            } elseif ($this->eq(0)->is('textarea')) {
                return $this->eq(0)->markup();
            } else {
                return $this->eq(0)->attr('value');
            }
        } else {
            $_val = null;
            foreach ($this->stack(1) as $node) {
                $node = pq($node, $this->getDocumentID());
                if (is_array($val) && in_array($node->attr('type'), array('checkbox', 'radio'))) {
                    $isChecked = in_array($node->attr('value'), $val) || in_array($node->attr('name'), $val);
                    if ($isChecked) {
                        $node->attr('checked', 'checked');
                    } else {
                        $node->removeAttr('checked');
                    }
                } elseif ($node->get(0)->tagName == 'select') {
                    if (!isset($_val)) {
                        $_val = array();
                        if (!is_array($val)) {
                            $_val = array((string) $val);
                        } else {
                            foreach ($val as $v) {
                                $_val[] = $v;
                            }
                        }
                    }
                    foreach ($node['option']->stack(1) as $option) {
                        $option = pq($option, $this->getDocumentID());
                        $selected = false;
                        // XXX: workaround for string comparsion, see issue #96
                        // http://code.google.com/p/phpquery/issues/detail?id=96
                        $selected = is_null($option->attr('value')) ? in_array($option->markup(), $_val) : in_array($option->attr('value'), $_val);
                        //						$optionValue = $option->attr('value');
                        //						$optionText = $option->text();
                        //						$optionTextLenght = mb_strlen($optionText);
                        //						foreach($_val as $v)
                        //							if ($optionValue == $v)
                        //								$selected = true;
                        //							else if ($optionText == $v && $optionTextLenght == mb_strlen($v))
                        //								$selected = true;
                        if ($selected) {
                            $option->attr('selected', 'selected');
                        } else {
                            $option->removeAttr('selected');
                        }
                    }
                } elseif ($node->get(0)->tagName == 'textarea') {
                    $node->markup($val);
                } else {
                    $node->attr('value', $val);
                }
            }
        }
        return $this;
    }