get/set element value and more
$ component install nickjackson/val
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'
.value()
- get value.value(str)
- set value
.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
.value()
- get value.value(str)
- set value
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)
})
- Test on other browsers. Currently only tested on Chrome OSX.
MIT