Skip to content
This repository has been archived by the owner on Sep 10, 2023. It is now read-only.

Added callbacks token un/selection events #776

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bower.json
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name" : "jquery-tokeninput",
"version" : "1.6.2",
"version" : "1.7.2",
"description" : "Tokeninput is a jQuery plugin which allows your users to select multiple items from a predefined list, using autocompletion as they type to find each item.",
"main" : [ "./src/jquery.tokeninput.js", "./styles/token-input.css" ],
"homepage" : "http://loopj.com/jquery-tokeninput",
Expand Down
4 changes: 2 additions & 2 deletions build/jquery.tokeninput.min.js

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions demo.html
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -746,5 +746,32 @@ <h2>Exclude Current Tokens From Autocomplete</h2>
</script>
</div>

<h2>Token Selection Callbacks</h2>
<div>
<input type="text" id="demo-input-local-selection-callbacks" name="blah" />
<input type="button" value="Submit" />
<script type="text/javascript">
$(document).ready(function() {
$("#demo-input-local-selection-callbacks").tokenInput([
{id: 7, name: "Ruby"},
{id: 11, name: "Python"},
{id: 13, name: "JavaScript"},
{id: 17, name: "ActionScript"},
{id: 19, name: "Scheme"},
{id: 23, name: "Lisp"},
{id: 29, name: "C#"},
{id: 31, name: "Fortran"},
{id: 37, name: "Visual Basic"},
{id: 41, name: "C"},
{id: 43, name: "C++"},
{id: 47, name: "Java"}
], {
onSelectedToken: function(element,token) { console.log("Token Selected: ", element,token); },
onUnselectedToken: function(element,token) { console.log("Token Unselected: ", element,token); }
});
});
</script>
</div>

</body>
</html>
Empty file modified package.json
100644 → 100755
Empty file.
13 changes: 13 additions & 0 deletions src/jquery.tokeninput.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
onFreeTaggingAdd: null,
onDelete: null,
onReady: null,
onSelectedToken: null,
onUnselectedToken: null,

// Other settings
idPrefix: "token-input-",
Expand Down Expand Up @@ -695,6 +697,11 @@
token.addClass($(input).data("settings").classes.selectedToken);
selected_token = token.get(0);

// Call selected token callback
if (typeof($(input).data("settings").onSelectedToken) === "function") {
$(input).data("settings").onSelectedToken.call(null,selected_token, $.data(selected_token,"tokeninput"));
}

// Hide input box
input_box.val("");

Expand All @@ -706,6 +713,7 @@
// Deselect a token in the token list
function deselect_token (token, position) {
token.removeClass($(input).data("settings").classes.selectedToken);
unselected_token = selected_token;
selected_token = null;

if(position === POSITION.BEFORE) {
Expand All @@ -721,6 +729,11 @@

// Show the input box and give it focus again
focusWithTimeout(input_box);

// Call selected token callback
if (typeof($(input).data("settings").onUnselectedToken) === "function") {
$(input).data("settings").onUnselectedToken.call(null,unselected_token,$.data(unselected_token,"tokeninput"));
}
}

// Toggle selection of a token in the token list
Expand Down