# web-element

> Generate HTML for a web page

Latest version **1.6.0** (published 2021-06-01) · ISC license · 0 weekly downloads

## Install

```sh
npm install web-element
pnpm add web-element
yarn add web-element
bun add web-element
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.6.0 |
| Published | 2021-06-01 |
| First published | 2016-09-14 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 31.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Maintainers | erikpukinskis |

## Links

- npm: https://www.npmjs.com/package/web-element
- Repository: https://github.com/erikpukinskis/web-element
- Issues: https://github.com/erikpukinskis/web-element/issues
- npm.io page: https://npm.io/package/web-element

## Dependencies (1)

- [module-library](https://npm.io/package/module-library.md) *

## Recent versions

- 1.6.0 (latest) — 2021-06-01
- 1.5.0 — 2021-05-29
- 1.4.0 — 2021-05-09
- 1.3.0 — 2020-12-27
- 1.2.0 — 2020-08-19
- 1.1.0 — 2020-07-13
- 0.109.0 — 2019-04-05
- 0.108.0 — 2019-02-24
- 0.107.0 — 2019-02-24
- 0.106.0 — 2019-01-28
- 0.105.0 — 2018-11-13
- 0.104.0 — 2018-11-13
- 0.103.0 — 2018-10-29
- 0.102.0 — 2018-10-06
- 0.101.0 — 2018-08-25
- … 48 more at https://npm.io/package/web-element/versions

## README

**web-element** generates HTML. That's it.

You can pass in a selector, attributes, and contents and it gives you back some HTML.

```javascript
var element = require("web-element")

element(
  "button.small", 
  {onclick: "doSomething()"},
  "DO IT"
).html()
```

Should generate:

```html
<button class="small" onclick="doSomething()">DO IT</button>
```

You can also pass arrays and it will try to turn the elements into HTML.

```javascript
element([
  "some text",
  ".some.classes",
  "<some>HTML</some>",
  element("a", {href: "/"}, "ok")
]).html()
```

Should generate:

```html
<div>
  <div>some text</div>
  <div class="some classes"></div>
  <some>HTML</some>
  <a href="/">ok</a>
</div>
```

## Inline styles

```javascript
element("h1", "Big Big News",
  element.style({
    display: "inline"
  })
)
```

generates:

```html
<h1 style="display: inline">Big Big News</h1>
```

## Templates

If you want to build an element over and over, element.template will return a function that can be used to generate elements:

```javascript
var mealTemplate = element.template(
  ".meal",
  element.style({
    "box-shadow": "1px 1px 10px #eee",
    "padding": "10px",
  }),
  function(id, title) {
    var photo = element("img", {src: "/images/"+id+".jpg"})
    var button = element("a.button", {href: "/buy/"+id}, "Deliver")
    this.addChild(photo)
    this.addChild(title)
    this.addChild(button)
  }
)

var page = element("body")

db.query("SELECT id, title FROM meals", function(id, title) {
  var meal = mealTemplate(id, title)
  page.addChild(meal)
})

page.addToHead(element.stylesheet(mealTemplate))

response.send(page.html())
```

## Media queries

```javascript
var responsive = element.style(
  ".responsive",
  {
    "@media (max-width: 600px)": {
      "font-size": "0.8em"
    }
  }
)
```

## Descendant styles

You can also include second level styles and pseduoelements. Don't forget to escape your content strings!

```javascript
var callout = element.style(
  ".callout",
  {
    "border-bottom": "2pt solid cyan",

    ".error": {
      "border-color": "red",
    },

    "::after": {
      "content": "\"\"",
      "width": "6pt",
      "height": "6pt",
      "background": "cyan",
      "vertical-align": "-2pt",
    }
  }
)
```

generates:

```css
.callout {
  border-bottom: 2pt solid cyan;
}

.callout.error {
  border-color: red;
}

.callout::after {
  content: ".";
  width: 6pt;
  height: 6pt;
  background: cyan;
  vertical-align: -2pt;
}
```

## Methods

```javascript
var el = element(element.tag("zoomable-image"))

el.addSelector(".foo")

el.appendStyles({
  "display": "none"
})

el.onclick("alert(\"hi\")")

el.addChild(element("baby element"))

el.assignId() // el.id == "fj29"

el.addAttribute("src", "foo.png")

el.addAttributes({
  width: 320,
  height: 240,
})
```

Generates

```html
<zoomable-image class="foo" style="display: none" onclick="alert(\"hi\")" id="fj29" src="foo.png" width="320" height="240">
  <div>baby element</div>
</zoomable-image>
```

If you have variable content and you want to ensure it is not interpreted as a tagname or a selector, you can use element.raw or element.escape:

```javascript
var someHtml = "<img/>"
element(
  element.raw(someHtml))
```
produces:
```html
<div><img/></div>
```
whereas
```javascript
var treatAsText = "<img/>"
element(
  element.escape(treatAsText))
```
produces:
```html
<div>&lt;img/&gt;</div>
```

## Why?

When someone tries to learn how to build web apps, they have to learn Javascript, HTML, and CSS all at once. Web-element lets them build web pages using only JavaScript.

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