Sha256: 387fd27abd365d12a4b95d3a210c7a114cfb115c5237a11cdfc028174eb97ce2

Contents?: true

Size: 1.92 KB

Versions: 80

Compression:

Stored size: 1.92 KB

Contents

/**
 * Module dependencies.
 */

var typeOf = require('type');

/**
 * Set or get `el`'s' value.
 *
 * @param {Element} el
 * @param {Mixed} val
 * @return {Mixed}
 * @api public
 */

module.exports = function(el, val){
  if (2 == arguments.length) return set(el, val);
  return get(el);
};

/**
 * Get `el`'s value.
 */

function get(el) {
  switch (type(el)) {
    case 'checkbox':
    case 'radio':
      if (el.checked) {
        var attr = el.getAttribute('value');
        return null == attr ? true : attr;
      } else {
        return false;
      }
    case 'radiogroup':
      for (var i = 0, radio; radio = el[i]; i++) {
        if (radio.checked) return radio.value;
      }
      break;
    case 'select':
      for (var i = 0, option; option = el.options[i]; i++) {
        if (option.selected) return option.value;
      }
      break;
    default:
      return el.value;
  }
}

/**
 * Set `el`'s value.
 */

function set(el, val) {
  switch (type(el)) {
    case 'checkbox':
    case 'radio':
      if (val) {
        el.checked = true;
      } else {
        el.checked = false;
      }
      break;
    case 'radiogroup':
      for (var i = 0, radio; radio = el[i]; i++) {
        radio.checked = radio.value === val;
      }
      break;
    case 'select':
      for (var i = 0, option; option = el.options[i]; i++) {
        option.selected = option.value === val;
      }
      break;
    default:
      el.value = val;
  }
}

/**
 * Element type.
 */

function type(el) {
  var group = 'array' == typeOf(el) || 'object' == typeOf(el);
  if (group) el = el[0];
  var name = el.nodeName.toLowerCase();
  var type = el.getAttribute('type');

  if (group && type && 'radio' == type.toLowerCase()) return 'radiogroup';
  if ('input' == name && type && 'checkbox' == type.toLowerCase()) return 'checkbox';
  if ('input' == name && type && 'radio' == type.toLowerCase()) return 'radio';
  if ('select' == name) return 'select';
  return name;
}

Version data entries

80 entries across 80 versions & 1 rubygems

Version Path
ende-0.5.22 components/component/value/1.1.0/index.js
ende-0.5.21 components/component/value/1.1.0/index.js
ende-0.5.20 components/component/value/1.1.0/index.js
ende-0.5.19 components/component/value/1.1.0/index.js
ende-0.5.18 components/component/value/1.1.0/index.js
ende-0.5.17 components/component/value/1.1.0/index.js
ende-0.5.16 components/component/value/1.1.0/index.js
ende-0.5.15 components/component/value/1.1.0/index.js
ende-0.5.14 components/component/value/1.1.0/index.js
ende-0.5.13 components/component/value/1.1.0/index.js
ende-0.5.12 components/component/value/1.1.0/index.js
ende-0.5.10 components/component/value/1.1.0/index.js
ende-0.5.9 components/component/value/1.1.0/index.js
ende-0.5.8 components/component/value/1.1.0/index.js
ende-0.5.7 components/component/value/1.1.0/index.js
ende-0.5.6 components/component/value/1.1.0/index.js
ende-0.4.25 vendor/components/component-value/index.js
ende-0.5.5 components/component/value/1.1.0/index.js
ende-0.5.4 components/component/value/1.1.0/index.js
ende-0.5.3 components/component/value/1.1.0/index.js