get/set element value - inspired by jQuery .val()
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.

Readme.md

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