I have a function and I want to adapt it to work without needing the element ID ( document.getElementById("slides");
). I saw that it is possible to do this using the NEW
operator, but I had difficulties. Do you have any practical examples that might help?
pg = 0;
sg = 20000;
ft = document.getElementById("slides");
sn = ft.getElementsByTagName("div");
tt = sn.length-1;
st = ["content-slide show-slide", "content-slide"];
//onclick
function slide(i){"proximo"==i&&md_n(),"anterior"==i&&md_p()}
//próximo
function md_n(){
if(pg < tt){
pg=pg+1;
sn.item(pg).setAttribute("class", st[0]);
if(pg > 0){
sn.item(pg-1).setAttribute("class", st[1]);
}
}else if(pg > 0 && pg == tt){
pg=0;
sn.item(pg).setAttribute("class", st[0]);
sn.item(tt).setAttribute("class", st[1]);
}
console.log("slide: "+pg);
}
//anterior
function md_p(){
if(pg > 0){
pg=pg-1;
sn.item(pg).setAttribute("class", st[0]);
if(pg > -1){
sn.item(pg+1).setAttribute("class", st[1]);
}
}else if(pg > -1 && pg != tt){
pg=sn.length-1;
sn.item(pg).setAttribute("class", st[0]);
sn.item(pg-(tt)).setAttribute("class", st[1]);
}
console.log("slide: "+pg);
}
//play
start = setInterval(function(){md_n()}, sg);