Skip to content
This repository has been archived by the owner on May 2, 2019. It is now read-only.

Latest commit

 

History

History
99 lines (75 loc) · 1.74 KB

Readme.md

File metadata and controls

99 lines (75 loc) · 1.74 KB

val

get/set element value and more

Installation

$ component install nickjackson/val

Example

var val = require('val');

var el = document.querySelector('#textbox');

val(el).value('foo bar baz')
// set value to 'foo bar baz'

val(el).value()
//= 'foo bar baz'

API

input

  • .value() - get value
  • .value(str) - set value

input/checkbox

  • .value() - get value
    • returns value or true if checked
    • returns undefined if not checked.
  • .value(str) - sets value
  • .checked() - gets checked state
  • .checked(boolean) - sets checked state
  • .checkedValue() - gets value irrelevant of checked

textarea

  • .value() - get value
  • .value(str) - set value

select

Get selected option - Multiple Disabled

val(el).value() 
//= 'item-1'
val(el).text()
//= 'Item 1'

Set selected option - Multiple Disabled

// if array is specified, only the first index is set to selected
	
val(el).value('item-1')
val(el).value(['item-3', 'item-4'])
val(el).text('Item 1')
val(el).text(['Item 3', 'Item 4'])

Get selected option - Multiple Enabled

val(el).value() 
//= ['item-1', 'item-3']
val(el).text()
//= ['Item 1', 'Item 3']

Set selected option - Multiple Enabled

val(el).value('item-1')
val(el).value(['item-3', 'item-4'])
val(el).text('Item 1')
val(el).text(['Item 3', 'Item 4'])

Set options

val(el).options(['Item 1', 'Item 2'])
	
// you must specify a text tag.
var opt = {text: 'Item 1', value: 'item-1', selected: false}
val(el).options([opt])

Loop options

val(el).options(function(option, selected){
  console.log(option, 'is', selected)
})

Todo

  • Test on other browsers. Currently only tested on Chrome OSX.

License

MIT