What I try to do is that the last image is put at the beginning when you click on the button, but the button only works from the second click, what is my error?
function click()
{
var button=document.getElementsByTagName("button");
button[0].addEventListener("click",pasarimg,false);
}
function pasarimg()
{
var padre=document.getElementById("padre");
var ultimo=padre.lastChild;
var primero=padre.firstChild;
padre.insertBefore(ultimo,primero)
}
window.onload=click
#padre
{
position: relative;
right: 0;
margin: auto;
width: 100%;
white-space: nowrap;
}
.img
{
position: relative;
width: 16.166%;
display: inline-block;
vertical-align:middle;
text-align: center;
padding: 12% 0;
background-repeat: no-repeat;
background-position: 50% 50%;
background-size: cover;
}
button
{
width: 50px;
height: 50px;
}
<div id="padre">
<div class="img" style="background-image: url(http://img.fenixzone.net/i/rBbHXpy.jpeg)"></div>
<div class="img" style="background-image: url(http://img.fenixzone.net/i/rBbHXpy.jpeg)"></div>
<div class="img" style="background-image: url(http://img.fenixzone.net/i/rBbHXpy.jpeg)"></div>
<div class="img" style="background-image: url(http://i.imgur.com/xV5iygm.png"></div>
</div>
<button >click</button>