# onlyjs

> Allows HTML and CSS to be written neatly with Javascript object literals

Latest version **0.0.3** (published 2017-04-14) · ISC license · 0 weekly downloads

## Install

```sh
npm install onlyjs
pnpm add onlyjs
yarn add onlyjs
bun add onlyjs
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.3 |
| Published | 2017-04-14 |
| First published | 2017-03-23 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Jacob Prinz |
| Maintainers | jacobprinz |
| Keywords | html, css, json, object, literals, only, js, onlyjs |

## Links

- npm: https://www.npmjs.com/package/onlyjs
- Repository: https://github.com/jeprinz/onlyjs
- Homepage: https://github.com/jacob5555555555/onlyjs#readme
- Issues: https://github.com/jacob5555555555/onlyjs/issues
- npm.io page: https://npm.io/package/onlyjs

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.0.3 (latest) — 2017-04-14
- 0.0.2 — 2017-03-23
- 0.0.1 — 2017-03-23
- 0.0.0 — 2017-03-23

## README

# only.js: Write everything in javascript
only.js allows HTML and CSS to be written inline in Javascript code using JSON representation.

For example, this HTML:
```HTML
<div>
  <p>Food:</p>
  <ul class="cake">
    <li>Milk</li>
    <li>Spinach</li>
    <li>Tacos</li>
    <li>Peas</li>
  </ul>
</div>
```
can become this javascript:
```javascript
only.setHtml([
  {div: [
    {ul: [
        {li: "Milk"},
        {li: "Spinach"},
        {li: "Tacos"},
        {li: "Peas"}
      ],
      class: "cake"
    }]//ul
  }//div
]);
```

###CSS
This CSS:
```CSS
.cake {
  color: red;
  display: inline-block;
}
```
becomes this javascript:
```javascript
only.makeCss(".cake",{
  color: "red",
  display: "inline-block"
});
```

or, put css style inline with javascript
```javascript
only.setHtml({
  p: "Hello World",
  css: {
    color: "red",
    display: "inline-block"
  }
})
```

###Inline javascript
This allows you to put any code you want into your HTML representation directly,
just like a templating engine but in Javascript:
```javascript
only.setHtml([
    {p: String(new Date())}
]);
```
Will be displayed as:
  <p>Sat Jul 25 2015 15:40:38 GMT-0400 (Eastern Daylight Time)</p>

###Keep all code for one component together
This HTML+javascript+CSS, spread between three files:
```HTML
<body>
  <!-- ... -->
  <button id="button1" class="button"></button>
  <!-- ... -->
</body>
```
```javascript
//...
$(".button1").click(
	alert("I just got clicked!");
)
//...
```
```CSS
/* ... */
.button {
	border-style: solid;
	border-width: 5px;
}
/* ... */
```
can become just one thing in one file:
```javascript
//create button
var button = only.html(
  {button: "ima button",
		css: {
			borderStyle: "solid",
			borderWidth: "5px"
		}
	}
);

//add listener
button.addEventListener("click", function(e){
  console.log("button clicked!");
})

//add to page
only.setHtml([
  button
])
```

###Reuse Elements Easily
To reuse an element, all you need to do is save it in a var:
```javascript
//create reusable error message
var errorMsg = {
	div: [
		{p: "there has been an error"},
		{img: "",
			src: "error.png"}
	],
	style: "border-width:10;border-style:solid;"
};

//use it:
only.setHtml([
	//...
	errorMsg,
	//... more stuff
	errorMsg
]);

```


###Another example
Simple user interaction:

```javascript
var input = only.html({input: ""});
var submit = only.html({button: "Submit Name"})
var response = only.html({p: ""});

//no need for an id on the button
submit.onclick = function(){
	var name = input.value;
	response.innerHTML = "Hello " + name, " how are you today?";
}

only.setHtml([
	{p: "Please enter your name"},
	input,
	submit,
	response
]);
```

---
_Source: https://npm.io/package/onlyjs · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
