## [Kutty in a Nutshell](#introduction)
Kutty is a library that allows you to access modern browser features directly from HTML, rather than using
javascript.
To understand kutty, first lets take a look at an anchor tag:
``` html
Blog
```
This anchor tag tells a browser:
> "When a user clicks on this link, issue an HTTP GET request to '/blog' and load the response content
> into the browser window".
With that in mind, consider the following bit of HTML:
``` html
Click Me!
```
This tells kutty:
> "When a user clicks on this div, issue an HTTP POST request to '/clicked' and load the response content into the inner
> html of this element"
Kutty extends the basic idea of that anchor tag, but opens up many more possibilities:
* Any element can issue a HTTP request
* Any event can trigger the request (not just clicks or form submissions)
* HTTP requests are done via AJAX
* Different HTTP verbs can used
* The response replaces the content of the element, rather than the entire page
When you are using kutty, you respond to the AJAX calls with *HTML* rather than *JSON*, often just a small amount of
HTML rather than the whole page.
Note that if you prefer, you can use the `data-` prefix when using kutty:
``` html
Click Me!
```
## [Installing](#installing)
Kutty is a dependency-free javascript library. It can be used via [NPM](https://www.npmjs.com/) as "`kutty.org`" or
downloaded or included from [unpkg](https://unpkg.com/browse/kutty.org/):
``` html
```
## [AJAX](#ajax)
One of the primary features kutty provides are attributes to allow you to issue AJAX requests directly from HTML:
* [kt-get](/attributes/kt-get) - Issues a `GET` request to the given URL
* [kt-post](/attributes/kt-post) - Issues a `POST` request to the given URL
* [kt-put](/attributes/kt-put) - Issues a `PUT` request to the given URL (see [details](#kutty-request-details))
* [kt-patch](/attributes/kt-patch) - Issues a `PATCH` request to the given URL (see [details](#kutty-request-details))
* [kt-delete](/attributes/kt-delete) - Issues a `GET` request to the given URL (see [details](#kutty-request-details))
Each of these attributes takes a URL to issue an AJAX request to. The element will issue a request of the specified
type to the given URL when the element is [triggered](#triggers):
```html
Put To Messages
```
This tells the browser:
> When a user clicks on this div, PUT to the URL /messages and load the response into the div
### [Triggering Requests](#triggers)
By default AJAX requests are triggered by the "natural" event of an element:
* `input`, `textarea` & `select`: the `change` event
* `form`: the `submit` event
* everything else: the `click` event
If you don't want the request to happen on the default event, you can use the [kt-trigger](/attributes/kt-trigger)
attribute to specify the event of interest. Here is a `div` that posts to `/mouse_entered`
when a mouse enters it:
```html
[Here Mouse, Mouse!]
```
If you want a request to only happen once, you can use the [kt-trigger-once](/attributes/kt-trigger-once) attribute:
```html
[Here Mouse, Mouse!]
```
There are two additional modifiers you can use for trigger:
* [kt-trigger-changed-only](/attributes/kt-trigger-changed-only) - when set to `true` the element will only issue a
request if its value has changed
* [kt-trigger-delay](/attributes/kt-trigger-delay) - tells kutty to wait the given amount of time (e.g. `1s`) before
issuing the request. If the event triggers again, the countdown is reset.
You can use these two attributes to implement a common UX pattern, [Live Search](/demo/live-search):
```html
```
This input will issue a request 500 milliseconds after a key up event if the input has been changed and puts the results
into the `div#search-results`.
#### [Special Events](#special-events)
kutty provides a few special events for use in [kt-trigger](/attributes/kt-trigger):
* `load` - fires once when the element is first loaded
* `revealed` - fires once when an element first scrolls into the viewport
You can also use custom events to trigger requests if you have an advanced use case.
#### [Polling](#polling)
If you want an element to poll the given URL rather than wait for an event, you can use the `every` syntax:
```html
```
This tells kutty
> Every 2 seconds, issue a GET to /news and load the response into the div
#### [Server Sent Events](#sse)
[Server Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events) are
a way for servers to send events to browsers. It provides a higher-level mechanism for communication between the
server and the browser than websockets.
If you want an element to respond to a Server Sent Event via kutty, you need to do two things:
1. Define an SSE source. To do this, add a [kt-sse-src](/attributes/kt-sse-src) attribute on a parent element
that specifies the URL from which Server Sent Events will be received.
2. Specify the Server Sent Event that will trigger the element, with the prefix `sse:`
Here is an example:
```html
```
Depending on your implementation, this may be more efficient than the polling example above since the server would
notify the div if there was new news to get, rather than the steady requests that a poll causes.
### [Request Indicators](#indicators)
When an AJAX request is issued it is often good to let the user know that something is happening, since the browser
will not give them any feedback. You can accomplish this in kutty by using the [kt-indicator](/attributes/kt-indicator)
attribute, the `kutty-show-indicator` class and some CSS.
By default the `kutty-show-indicator` class will be put on the element issuing the request. This can be used to show a
spinner gif, for example:
```html
```
If you want the `kutty-show-indicator` class added to a different element, you can use the [kt-indicator](/attributes/kt-indicator)
attribute with a CSS selector to do so:
```html
```
### [Targets](#targets)
If you want the response to be loaded into a different element other than the one that made the request, you can
use the [kt-target](/attributes/kt-target) attribute, which takes a CSS selector. Looking back at our Live Search example:
```html
```
You can see that the results from the search are going to be loaded into `div#search-results`, rather than into the
input tag.
### [Swapping](#swapping)
kutty offers a few different ways to swap the HTML returned into the DOM. By default, the content replaces the
`innerHTML` of the target element. You can modify this by using the [kt-swap](/attributes/kt-swap) attribute
with any of the following values:
* `innerHTML` - the default, puts the content inside the target element
* `outerHTML` - replaces the entire target element with the returned content
* `prepend` - prepends the content before the first child inside the target
* `prependBefore` - prepends the content before the target in the targets parent element
* `append` - appends the content after the last child inside the target
* `appendAfter` - appends the content after the target in the targets parent element
#### Out of Band Swaps
If you want to swap content from a response directly into the DOM by using the `id` attribute you can use the
[kt-swap-oob](/attributes/kt-swap-oob) attribute in the *response* html:
```html