Code Monkey home page Code Monkey logo

selectables's Introduction

Selectables

Select elements in webpage with mouse.

Simple js without external dependencies.

Usage:

dr = new Selectables({
      zone:'#div',
      elements: 'li',     
      onSelect:function(element){
         console.log(element);
      }
});
  
//later
dr.disable();
 
// enable again
dr.enable();

// set options
dr.options.key='altKey';

##Options and Callbacks:

{
    // root element whith selectables.
    zone: "#wrapper",

    //  items to be selectable .list-group, #id > .class,'htmlelement' - valid querySelectorAll
    elements: "li",

    // class name to apply to seleted items        
    selectedClass: 'active',

    //  event on selection start        
    start: function (e) {
       console.log('Starting selection on ' + this.elements + ' in ' + this.zone);
    },

    // event on selection end        
    stop: function (e) {
       console.log('Finished selecting   ' + this.elements + ' in ' + this.zone);
    },

    // event fired on every item when selected.
    onSelect: function (el) {
        console.log(el)
       console.log('onselect', el);
    },

    // event fired on every item when selected.
    onDeselect: function (el) {
       console.log('ondeselect', el);
    },

    // activate using optional key
    key: false, //'altKey,ctrlKey,metaKey,false   

    // add more to selection
    moreUsing: 'shiftKey', //altKey,ctrlKey,metaKey

    //false to .enable() at later time   

    enabled: true

}

Example usage

Toggle multiple checkboxes in list

onSelect: function (el) {
    el.querySelector('input').setAttribute('checked', 'checked');
},
onDeselect: function (el) {
    el.querySelector('input').removeAttribute('checked');
}
                    

Install

npm install selectables

or

bower install selectables 

Example extension

Custom method declared after selectables.js is loaded.

    Selectables.prototype.selectAll = function () {
        var opt = this.options;
        this.foreach(document.querySelector(opt.zone).querySelectorAll(opt.elements), function (el) {
            if(!el.classList.contains(opt.selectedClass){
                el.classList.add(opt.selectedClass);
                opt.onSelect && opt.onSelect(el);
            }   
        });
    };

Notes

  1. Multiple instances on different zones and items are possible, but it is good to enable them only when needed.

  2. When selecting, "noselect" class gets applied to the zone, to avoid unwanted text selections.

Why?

Created in effort to drop code size and dependency on jquery && jquery.ui for small projects.

selectables's People

Contributors

p34eu avatar

Watchers

James Cloos avatar

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.