@kickscondor/umbrellajs
Version:
Lightweight and intuitive javascript library
100 lines (64 loc) • 2.49 kB
Markdown
Add some html before each of the matched elements.
```js
.before(html)
.before('<div>')
.before(u('<div>'))
.before(u('<div>').first()) // Same as document.createElement('div')
.before(u('<div></div><div></div>'))
.before(function(){})
.before(function(el){}, elements)
.append(function(el){}, 10)
```
`html = ""`:
- Any of these elements:
- **string** containing the html that is going to be inserted
- **instance of Umbrella**
- **HTML node**
- **array** containing HTML nodes
- A callback that returns any of the previous. It gets passed these parameters:
- **el**: the current element from the elements parameter, {} if none is specified and i if elements is number
- **i**: the index of the current element
`elements = [{}]` (optional): It can be any of the following:
- An array of elements that will be passed to the callback. The callback is executed once per element, and all of them are added consecutively.
- A CSS selector, so the function will be executed once per matched element.
- A number, in which case the function will be executed that number of times
`u`: returns the same instance of Umbrella JS
Add a header to each of the articles
```js
u("article").after("<header>Hello world</header>");
```
Add three elements before the link. All of these methods are equivalent:
```js
// Add them all like a single string
u("a.main").before("<a>One</a><a>Two</a><a>Three</a>");
// Add them in a chain
u("a.main").before("<a>One</a>").before("<a>Two</a>").before("<a>Three</a>");
// Add them with a function parameter
var cb = function(txt){ return "<a>" + txt + "</a>" };
u("a.main").before(cb, ["One", "Two", "Three"]);
// Same as the previous one but with ES6
u("a.main").before(txt => `<a>${ txt }</a>`, ["One", "Two", "Three"]);
```
They all result in:
```html
<a>One</a>
<a>Two</a>
<a>Three</a>
<a class="main"></a>
<!-- previous data -->
```
You can also add some events to them by creating an html node:
```js
function greeting(){ alert("Hello world"); }
u("a.main").before(function(){
return u('<a>').addClass('hi').on('click', greeting).html("Greetings!");
});
```
[.after()](
[.append()](
[.prepend()](