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

get/set element value - inspired by jQuery .val()

Notifications You must be signed in to change notification settings

nickjackson/val

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

get/set element value - inspired by jQuery .val()

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •