Skip to content

Commit

Permalink
Add unbind().
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Mar 7, 2024
1 parent 8c4ea42 commit 3fe56ff
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/floatype.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ <h1>floatype.js</h1>

// Example 1.
// Simple string results.
floatype(document.querySelector("textarea"), {
const f = floatype(document.querySelector("textarea"), {
onQuery: async (val) => {
// This callback returns an array of search results.
// Typically, this will be a server side fetch() request.
Expand All @@ -105,6 +105,8 @@ <h1>floatype.js</h1>
return WORDS.filter(s => s.startsWith(q)).slice(0, 10);
}
});

// f.unbind() to remove the binding.
</script>
</body>
</html>
6 changes: 6 additions & 0 deletions floatype.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ export function floatype(el, options = {}) {
el.value = el.value.substring(0, start) + val + (el.value[el.selectionStart] !== " " ? " " : "") + el.value.substring(el.selectionStart);
el.setSelectionRange(start + val.length + 1, start + val.length + 1);
}

return {
unbind: function() {
["input", "keydown", "blur"].forEach(k => el.removeEventListener(k, handleEvent));
}
}
}

export default floatype;

0 comments on commit 3fe56ff

Please sign in to comment.