| 1 | import htmx from "./htmx"; |
| 2 | |
| 3 | // add the class 'myClass' to the element with the id 'demo' |
| 4 | htmx.addClass(htmx.find("#demo"), "myClass"); |
| 5 | |
| 6 | // issue a GET to /example and put the response HTML into #myDiv |
| 7 | htmx.ajax("GET", "/example", "#myDiv"); |
| 8 | |
| 9 | // find the closest enclosing div of the element with the id 'demo' |
| 10 | htmx.closest(htmx.find("#demo"), "div"); |
| 11 | |
| 12 | // update the history cache size to 30 |
| 13 | htmx.config.historyCacheSize = 30; |
| 14 | |
| 15 | // override SSE event sources to not use credentials |
| 16 | htmx.createEventSource = function (url) { |
| 17 | return new EventSource(url, { withCredentials: false }); |
| 18 | }; |
| 19 | |
| 20 | // override WebSocket to use a specific protocol |
| 21 | htmx.createWebSocket = function (url) { |
| 22 | return new WebSocket(url, ["wss"]); |
| 23 | }; |
| 24 | |
| 25 | // defines a silly extension that just logs the name of all events triggered |
| 26 | htmx.defineExtension("silly", { |
| 27 | onEvent: function (name, evt) { |
| 28 | console.log("Event " + name + " was triggered!"); |
| 29 | } |
| 30 | }); |
| 31 | |
| 32 | // find div with id my-div |
| 33 | var div = htmx.find("#my-div"); |
| 34 | |
| 35 | // find div with id another-div within that div |
| 36 | var anotherDiv = htmx.find(div, "#another-div"); |
| 37 | |
| 38 | // find all divs |
| 39 | var allDivs = htmx.findAll("div"); |
| 40 | |
| 41 | // find all paragraphs within a given div |
| 42 | var allParagraphsInMyDiv = htmx.findAll(htmx.find("#my-div"), "p"); |
| 43 | |
| 44 | htmx.logAll(); |
| 45 | |
| 46 | // remove this click listener from the body |
| 47 | htmx.off("click", myEventListener); |
| 48 | |
| 49 | // remove this click listener from the given div |
| 50 | htmx.off("#my-div", "click", myEventListener); |
| 51 | |
| 52 | // add a click listener to the body |
| 53 | var myEventListener = htmx.on("click", function (evt) { |
| 54 | console.log(evt); |
| 55 | }); |
| 56 | |
| 57 | // add a click listener to the given div |
| 58 | var myEventListener = htmx.on("#my-div", "click", function (evt) { |
| 59 | console.log(evt); |
| 60 | }); |
| 61 | |
| 62 | const MyLibrary: any = null; |
| 63 | |
| 64 | htmx.onLoad(function (elt) { |
| 65 | MyLibrary.init(elt); |
| 66 | }); |
| 67 | |
| 68 | // returns 3000 |
| 69 | var milliseconds = htmx.parseInterval("3s"); |
| 70 | |
| 71 | // returns 3 - Caution |
| 72 | var milliseconds = htmx.parseInterval("3m"); |
| 73 | |
| 74 | document.body.innerHTML = "<div hx-get='/example'>Get it!</div>"; |
| 75 | // process the newly added content |
| 76 | htmx.process(document.body); |
| 77 | |
| 78 | // removes my-div from the DOM |
| 79 | htmx.remove(htmx.find("#my-div")); |
| 80 | |
| 81 | // removes .myClass from my-div |
| 82 | htmx.removeClass(htmx.find("#my-div"), "myClass"); |
| 83 | |
| 84 | htmx.removeExtension("my-extension"); |
| 85 | |
| 86 | // takes the selected class from tab2"s siblings |
| 87 | htmx.takeClass(htmx.find("#tab2"), "selected"); |
| 88 | |
| 89 | // toggles the selected class on tab2 |
| 90 | htmx.toggleClass(htmx.find("#tab2"), "selected"); |
| 91 | |
| 92 | // triggers the myEvent event on #tab2 with the answer 42 |
| 93 | htmx.trigger(htmx.find("#tab2"), "myEvent", { answer: 42 }); |
| 94 | |
| 95 | // gets the values associated with this form |
| 96 | var values = htmx.values(htmx.find("#myForm")); |