I would like to apply same piece of code to two objects in JavaScript.
When calling getElementsByClass
,there appears 2 objects in my website.So I would like to apply the same code for both of them.Currently I'm applying it to only one Object (text[0]) and I would like to implement it also to text[1] .
var text=document.getElementsByClassName("th");
var text =text[0];
var newDom = '';
var animationDelay = 6;
for(let i = 0; i < text.innerText.length; i )
{
newDom = '<span class="char">' (text.innerText[i] == ' ' ? ' ' : text.innerText[i]) '</span>';
}
text.innerHTML = newDom;
var length = text.children.length;
for(let i = 0; i < length; i )
{
text.children[i].style['animation-delay'] = animationDelay * i 'ms';
}
}
CodePudding user response:
I think you want to do the same thing with using item[0]
and item[1]
together.
You can create a function. Or call this function by iterating your items too.
var text=document.getElementsByClassName("th");
function myFunc(text) {
var newDom = '';
var animationDelay = 6;
for(let i = 0; i < text.innerText.length; i )
{
newDom = '<span class="char">' (text.innerText[i] == ' ' ? ' ' : text.innerText[i]) '</span>';
}
text.innerHTML = newDom;
var length = text.children.length;
for(let i = 0; i < length; i )
{
text.children[i].style['animation-delay'] = animationDelay * i 'ms';
}
}
}
myFunc(text[0]); // call functions with your items.
myFunc(text[1]);