Code Monkey home page Code Monkey logo

val's Introduction

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

val's People

Contributors

ianstormtaylor avatar manuelstofer avatar nickjackson avatar regular avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

val's Issues

Checkboxes value

I think val should return the value of checkboxes when they are checked and undefined when they are not. This would match the logic of the html form.

Return "example" for:

<input type="checkbox" value="example" checked>

And undefined for:

<input type="checkbox" value="example">

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.