Set Up
How to setup Selectable
Last updated
How to setup Selectable
Last updated
This will create a new instance and add any element found in the DOM with the 'ui-selectable'
class name and make them selectable.
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 option utilising a CSS3 selector string:
If you only want items within a certain container to be selectable then utilise the option:
This also confines the lasso within the defined container.
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:
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 option.
The problem is that the event listeners for an instance (mousedown
/ touchstart
and mouseup
/ touchend
) are attached to the 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 , the event listeners are instead attached to that container and will prevent conflicting with other instances.
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 option:
If you didn't define an element with the option, then you can retrieve it with document.body._selectable
.