@kickscondor/umbrellajs
Version:
Lightweight and intuitive javascript library
100 lines (65 loc) • 2.52 kB
Markdown
## .prepend()
Add some html as a child at the beginning of each of the matched elements.
```js
.prepend(html)
.prepend('<div>')
.prepend(u('<div>'))
.prepend(u('<div>').first()) // Same as document.createElement('div')
.prepend(u('<div></div><div></div>').nodes)
.prepend(function(){})
.prepend(function(el){}, elements)
.prepend(function(el){}, 10)
```
### Parameters
`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
### Return
`u`: returns the same instance of Umbrella JS
### Examples
Add a header to each of the articles
```js
u("article").prepend("<header>Hello world</header>");
```
Add three elements at the beginning of the list. All of these methods are equivalent:
```js
// Add them all like a single string
u("ul").prepend("<li>One</li><li>Two</li><li>Three</li>");
// Add them in a chain
u("ul").prepend("<li>Three</li>").append("<li>Two</li>").append("<li>One</li>");
// Add them with a function parameter
var cb = function(txt){ return "<li>" + txt + "</li>" };
u("ul").prepend(cb, ["One", "Two", "Three"]);
// Same as the previous one but with ES6
u("ul").prepend(txt => `<li>${ txt }</li>`, ["One", "Two", "Three"]);
```
They all result in:
```html
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<!-- previous data -->
</ul>
```
You can also add some events to them by creating an html node:
```js
function greeting(){ alert("Hello world"); }
u("a.main").prepend(function(){
return u('<a>').addClass('hi').on('click', greeting).html("Greetings!");
});
```
### Related
[.append()](#append) Add some html as a child at the end of each of the matched elements
[.before()](#before) Add some html before each of the matched elements.
[.after()](#after) Add some html as a sibling after each of the matched elements.