Set Up
How to setup Selectable
Creating an Instance
This will create a new instance and add any element found in the DOM with the 'ui-selectable'
class name and make them selectable.
Defining Selectable Items
If you don't add the 'ui-selectable'
class name to your items then simply tell the instance what to look for instead with the filter
option utilising a CSS3 selector string:
If you only want items within a certain container to be selectable then utilise the container
option:
This also confines the lasso within the defined container.
Adding Items
If your document doesn't have any selectable items yet, e.g. they're added asynchronously via an ajax call, then simply create a new instance as normal, then use the add()
method when they're available:
Multiple Instances
It's important to note that multiple instances of Selectable
on one page can conflict with each other if no container is defined with the container
option.
The problem is that the event listeners for an instance (mousedown
/ touchstart
and mouseup
/ touchend
) are attached to the container
element. If you have more than one instance and don't define a container, it defaults to document.body
so for example you'll have multiple mousedown
event listeners attached to document.body
and they'll all fire at the same time and hence all start
andend
events for each instance defined with on()
will fire at same time.
By defining a unique container for each instance with container
, the event listeners are instead attached to that container and will prevent conflicting with other instances.
Retrieving an Instance
If you didn't attach the instance to a variable then you can retrieve it by accessing the _selectable
property on the element defined by the container
option:
If you didn't define an element with the container
option, then you can retrieve it with document.body._selectable
.
Last updated