How to adapt this function to work without needing the element ID?

Asked

Viewed 55 times

0

I have a function and want to adapt it to work without needing the element ID (document.getElementById("slides");). I saw that there is the possibility to do this using the operator NEW but I had difficulties. Have some practical example that can 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);

2 answers

1

Without id, one option is to search for the class property. document.getElementsByClassName("nome_da_class");.

Note that this returns an array, independent of finding a single element.

Example

var e = document.getElementsByClassName("nome_da_class");
console.log(e[0]); // imprime no console o primeiro que encontrou.

To have everyone you found, make a loop of repetition

for (var i = 0; i < e.length; i++) {
    console.log(e[i]);
}

Otherwise you would have to read the entire document across all documents until you find specific properties of the element you are looking for like tag name, width, height, etc.

Be aware that I have posted only examples, without error handling. For example, at least you should check if something was found, if it did not return false, null or Undefined, at last.

1

This way you can select elements with standard css selectors.

querySelector() returns an element (the first one you find), or none (if there is no element with the desired property)

Already querySelectorAll() returns all elements (array) with the desired property, if any

var heyClass = document.querySelectorAll('.hey')[0]; // primeiro elemento
var heyData = document.querySelector('[data-name="yoo"]'); // primeiro elemento
var spanTag = document.querySelectorAll('span')[0]; // primeiro elemento

console.log(heyClass);
console.log(heyData);
console.log(spanTag);
<div class="hey"></div>
<div data-name="yoo"></div>
<span></span>

  • The problem with me selecting the elements for the class is that this way it would create conflict if I tried to implement more than one slide per page

  • Edited above, you can give the attributes you want, in this case use querySelectorAll(), as shown above. This way you can get all the slides from the page. I explained below how it works

  • Perfect the Migrate explanation, but note that when implementing two "Slides" on the page it is not possible for me to use the same function, as in the following example: -> https://jsbin.com/zihosivexo/edit?html,js output,

  • You need to cycle through all slides: http://jsfiddle.net/4huzr/2676/ . Try to implode like this at the beginning of js

  • Even so, it still hasn’t worked: https://jsbin.com/zihosivexo/edit?html,js,output

Browser other questions tagged

You are not signed in. Login or sign up in order to post.