Movatterモバイル変換


[0]ホーム

URL:


</> htmx

Javascript API

While it is not a focus of the library, htmx does provide a small API of helper methods, intended mainly forextension development or for working withevents.

Thehyperscript project is intended to provide more extensive scripting supportfor htmx-based applications.

Method -htmx.addClass()

This method adds a class to the given element.

Parameters

or

Example
// add the class 'myClass' to the element with the id 'demo'htmx.addClass(htmx.find('#demo'),'myClass');// add the class 'myClass' to the element with the id 'demo' after 1 secondhtmx.addClass(htmx.find('#demo'),'myClass',1000);

Method -htmx.ajax()

Issues an htmx-style AJAX request. This method returns a Promise, so a callback can be executed after the content has been inserted into the DOM.

Parameters

or

or

Example
// issue a GET to /example and put the response HTML into #myDivhtmx.ajax('GET','/example','#myDiv')// issue a GET to /example and replace #myDiv with the responsehtmx.ajax('GET','/example', {target:'#myDiv', swap:'outerHTML'})// execute some code after the content has been inserted into the DOMhtmx.ajax('GET','/example','#myDiv').then(()=>{// this code will be executed after the 'htmx:afterOnLoad' event,// and before the 'htmx:xhr:loadend' eventconsole.log('Content inserted successfully!');    });

Method -htmx.closest()

Finds the closest matching element in the given elements parentage, inclusive of the element

Parameters
Example
// find the closest enclosing div of the element with the id 'demo'htmx.closest(htmx.find('#demo'),'div');

Property -htmx.config

A property holding the configuration htmx uses at runtime.

Note that using ameta tag is the preferred mechanism for setting these properties.

Properties
Example
// update the history cache size to 30htmx.config.historyCacheSize=30;

Property -htmx.createEventSource

A property used to create newServer Sent Event sources. This can be updatedto provide custom SSE setup.

Value
Example
// override SSE event sources to not use credentialshtmx.createEventSource=function(url) {returnnew EventSource(url, {withCredentials:false});  };

Property -htmx.createWebSocket

A property used to create newWebSocket. This can be updatedto provide custom WebSocket setup.

Value
Example
// override WebSocket to use a specific protocolhtmx.createWebSocket=function(url) {returnnew WebSocket(url, ['wss']);  };

Method -htmx.defineExtension()

Defines a new htmxextension.

Parameters
Example
// defines a silly extension that just logs the name of all events triggeredhtmx.defineExtension("silly", {onEvent:function(name,evt) {console.log("Event "+name+" was triggered!")    }  });

Method -htmx.find()

Finds an element matching the selector

Parameters

or

Example
// find div with id my-divvardiv=htmx.find("#my-div")// find div with id another-div within that divvaranotherDiv=htmx.find(div,"#another-div")

Method -htmx.findAll()

Finds all elements matching the selector

Parameters

or

Example
// find all divsvarallDivs=htmx.findAll("div")// find all paragraphs within a given divvarallParagraphsInMyDiv=htmx.findAll(htmx.find("#my-div"),"p")

Method -htmx.logAll()

Log all htmx events, useful for debugging.

Example
htmx.logAll();

Method -htmx.logNone()

Log no htmx events, call this to turn off the debugger if you previously enabled it.

Example
htmx.logNone();

Property -htmx.logger

The logger htmx uses to log with

Value
Example
htmx.logger=function(elt,event,data) {if(console) {console.log("INFO:", event,elt,data);        }    }

Method -htmx.off()

Removes an event listener from an element

Parameters

or

Example
// remove this click listener from the bodyhtmx.off("click",myEventListener);// remove this click listener from the given divhtmx.off("#my-div","click",myEventListener)

Method -htmx.on()

Adds an event listener to an element

Parameters

or

Example
// add a click listener to the bodyvarmyEventListener=htmx.on("click",function(evt){console.log(evt); });// add a click listener to the given divvarmyEventListener=htmx.on("#my-div","click",function(evt){console.log(evt); });// add a click listener to the given div that should only be invoked oncevarmyEventListener=htmx.on("#my-div","click",function(evt){console.log(evt); }, { once:true});

Method -htmx.onLoad()

Adds a callback for thehtmx:load event. This can be used to process new content, for exampleinitializing the content with a javascript library

Parameters
Example
htmx.onLoad(function(elt){MyLibrary.init(elt);    })

Method -htmx.parseInterval()

Parses an interval string consistent with the way htmx does. Useful for plugins that have timing-related attributes.

Caution: Accepts an int followed by eithers orms. All other values useparseFloat

Parameters
Example
// returns 3000varmilliseconds=htmx.parseInterval("3s");// returns 3 - Cautionvarmilliseconds=htmx.parseInterval("3m");

Method -htmx.process()

Processes new content, enabling htmx behavior. This can be useful if you have content that is added to the DOMoutside of the normal htmx request cycle but still want htmx attributes to work.

Parameters
Example
  document.body.innerHTML="<div hx-get='/example'>Get it!</div>"// process the newly added contenthtmx.process(document.body);

Method -htmx.remove()

Removes an element from the DOM

Parameters

or

Example
// removes my-div from the DOMhtmx.remove(htmx.find("#my-div"));// removes my-div from the DOM after a delay of 2 secondshtmx.remove(htmx.find("#my-div"),2000);

Method -htmx.removeClass()

Removes a class from the given element

Parameters

or

Example
// removes .myClass from my-divhtmx.removeClass(htmx.find("#my-div"),"myClass");// removes .myClass from my-div after 6 secondshtmx.removeClass(htmx.find("#my-div"),"myClass",6000);

Method -htmx.removeExtension()

Removes the given extension from htmx

Parameters
Example
htmx.removeExtension("my-extension");

Method -htmx.swap()

Performs swapping (and settling) of HTML content

Parameters
Example
// swap #output element inner HTML with div element with "Swapped!" texthtmx.swap("#output","<div>Swapped!</div>", {swapStyle:'innerHTML'});

Method -htmx.takeClass()

Takes the given class from its siblings, so that among its siblings, only the given element will have the class.

Parameters
Example
// takes the selected class from tab2's siblingshtmx.takeClass(htmx.find("#tab2"),"selected");

Method -htmx.toggleClass()

Toggles the given class on an element

Parameters
Example
// toggles the selected class on tab2htmx.toggleClass(htmx.find("#tab2"),"selected");

Method -htmx.trigger()

Triggers a given event on an element

Parameters
Example
// triggers the myEvent event on #tab2 with the answer 42htmx.trigger("#tab2","myEvent", {answer:42});

Method -htmx.values()

Returns the input values that would resolve for a given element via the htmx value resolution mechanism

Parameters
Example
// gets the values associated with this formvarvalues=htmx.values(htmx.find("#myForm"));

[8]ページ先頭

©2009-2025 Movatter.jp