Clipboard
Bootstrap 5 Clipboard
Copy to clipboard feature for the latest Bootstrap 5. Let your users easily copy text or links with one click.
Note: Read the API tab to find all available options and advanced customization
*
*
UMD autoinits are enabled
by default. This means that you don't need to initialize
the component manually. However if you are using MDBootstrap ES format then you should pass
the required components to the initMDB
method.
Basic example
By adding button with data-mdb-clipboard-init
and
data-mdb-target
attributes you can easily make your text copyable.
Copy from element
There is no distinction, whether the element is an input
or simply a div
element.
Copy from data attribute
By adding data-mdb-clipboard-text
to the copying target, you can specify the text to be copied,
overriding the text from the targeted element.
Feedback example
Listen for copied.mdb.clipboard
event to perform actions, such as displaying feedback message to users.
Clipboard - API
Import
Importing components depends on how your application works. If you intend to use the MDBootstrap ES
format, you must
first import the component and then initialize it with the initMDB
method. If you are going to use the UMD
format,
just import the mdb-ui-kit
package.
Usage
Via data attributes
Using the Clipboard method doesn't require any additional JavaScript code - simply add
data-mdb-clipboard-init
attribute to
element you want to copy
and use other data attributes to set all options.
For ES
format, you must first import and call the initMDB
method.
Via JavaScript
Via jQuery
Note: By default, MDB does not include jQuery and you have to add it to the project on your own.
Options
Options can be passed via data attributes or JavaScript. For data attributes, append the option name to
data-mdb-
, as in data-mdb-clipboard-target=""
.
Name | Type | Default | Description |
---|---|---|---|
clipboardTarget
|
String | null | 'null' |
Points an element from which you will copy text. |
Methods
Name | Description | Example |
---|---|---|
dispose
|
Manually deletes an instance of clipboard |
myClipboard.dispose()
|
getInstance
|
Static method which allows you to get the clipboard instance associated to a DOM element. |
Clipboard.getInstance(myClipboardEl)
|
getOrCreateInstance
|
Static method which returns the clipboard instance associated to a DOM element or create a new one in case it wasn't initialized. |
Clipboard.getOrCreateInstance(myClipboardEl)
|
Events
Name | Description |
---|---|
copied.mdb.clipboard
|
This event fires immediately after copying text. |