Javascript - update all divs of the same class in a loop [duplicate] - javascript

This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
How do I add a delay in a JavaScript loop?
(32 answers)
Closed 5 years ago.
I am trying to update CSS for all divs (one by one) with a certain class using plain Javascript loop. Here it is:
var timerCoinRow = 0;
var rows = document.querySelectorAll('.coin-row');
for (var i=0; i<rows.length; i++)
{
timerCoinRow += 500;
obj = rows[i];
setTimeout(function ()
{
obj.style.opacity = 1;
}, timerCoinRow);
}
for some reason it does not work. here is JQuery version that works fine but I need it in plain "vanilla" Javascript:
var timerCoinRow = 0;
$('.coin-row').each(function ()
{
timerCoinRow += 500);
var obj =$(this);
setTimeout(function ()
{
obj.css('opacity', '1');
},timerCoinRow,obj);
});
When I place obj.style.opacity = 1; outside the timeout - all divs are updated but simultaneously while I need them to appear one after another with a pause of 500ms. Please help - it's somewhere near but ....

Related

Javascript to change color of all spans with certain ID [duplicate]

This question already has answers here:
Mulitple Elements with the same ID
(5 answers)
Closed 2 years ago.
I have the following code to change the color of text with a certain spanID. Currently, it only changes the first instance of the span and on subsequent instances. Any suggestions?
<script>
function spanColor() {
var x = document.getElementById('someId');
x.style.color = '#'+Math.random().toString(16).substr(-6);
}
</script>
If you want to return all the needed elements you should use querySelectorAll(#id)
But as you saw in the comments - ID is not the best way to get more than one item.
You should change the ID for a ClassName and use var x = document.getElementsByClassName("example");
So your code should look like this:
<script>
function spanColor() {
var x = document.getElementsByClassName('someClassname');
var i;
for (i = 0; i < x.length; i++) {
x[i].style.color = '#'+Math.random().toString(16).substr(-6);
}
}
</script>

Index based argument in for loop of addEventListener [duplicate]

This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Javascript infamous Loop issue? [duplicate]
(5 answers)
Closed 2 years ago.
I am adding some JS to an HTML, and I have a fragment of code similar to this:
<script>
function ButtonAction(index){
alert("My index is: "+index);
if(window_big){
// Use index to do something
}
else{
// Use index to do something else
}
}
function WindowResize(){
if(window.innerWidth > 1200){
window_big = true;
}
else{
window_big = false;
}
}
var window_big;
if(window.innerWidth > 1200){
window_big = true;
}
else{
window_big = false;
}
buttons = document.getElementsByClassName('MyButtons')
var i;
for (i = 0; i < buttons.length; i++) {
alert(i);
buttons[i].addEventListener("click",function(){ButtonAction(i);},false);
}
window.onresize = WindowResize;
</script>
The idea can be summarized like this:
There is a series of buttons in the page, stored in buttons[].
If the window is bigger than a certain size, those buttons should do one action, and if not, do another one.
To do said actions, I need the button[x].id. In fact, the initial intention was to set the listener to:
buttons[i].addEventListener("click",function(){ButtonAction(i);},false);
The problem is that I cannot retrieve the id, because the argument passed in the event listener seems to be always the last value i was set to.
May I ask for some help, please?

How would I get to increment the set timeout to apply to each classname pause? [duplicate]

This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 3 years ago.
I need help trying to increment my for loop so after 8 seconds my divs show not just one. I would prefer vanilla js if possible.
I have tried doing my own for loop and settimeout I have failed.
var googleAds = document.getElementsByClassName('pause');
var i;
for(var i = 0; i < googleAds.length; i++) {
setTimeout(function () {
googleAds[i].style.display = "block";
}, 8000);
}
isplaying after 8 seconds
You need to use the for loop inside the anonymous function for i to be in scope:
setTimeout(function () {
for(var i = 0; i < googleAds.length; i++) {
googleAds[i].style.display = "block";
}
}, 8000);

setTimeout in a loop execute all at the same time [duplicate]

This question already has answers here:
setTimeout() method inside a while loop [duplicate]
(8 answers)
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 3 years ago.
I want to open a link every 3 seconds. I am using the setTimeout function, but it doesn't work. All links will be opened once.
for(var i=0; i < url.length-1; i++) {
setTimeout(function(){
linkaddress=url[i];
window.open(linkaddress);
}, 3000);
}
Use "let" instead of "var" for block level scoping, and then multiply your time by i variable (more info). Code:
var url = ["https://domain1.com","https://www.domain2.com"],
timeout = 3; // Time in second
for(let i=1; i <= url.length; i++){
setTimeout(function(){
linkaddress=url[i-1];
window.open(linkaddress);
}, i * timeout * 1000);
}
EDIT: Note that this code uses EcmaScript 6 features
Use setInterval instead
url = ['a', 'b', 'c'];
var i = 0;
var interval = setInterval(function() {
if (i <= url.length - 1) {
///linkaddress = url[i];
//window.open(linkaddress);
console.log(url[i]);
i++;
} else {
clearInterval(interval);
}
}, 3000);

Adding class to div with onclick function [duplicate]

This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 7 years ago.
I tried to find some way to solve my problem, which is to add class to the divs when I click on them, but I can't make it work.
var el = document.getElementsByClassName('applications');
var i;
for (i = 0; i < el.length; i++) {
el[i].addEventListener("click", function() {
if (el[i]) {
el[i].className += el[i].className ? ' openDiv' : 'openDiv';
}
});
}
I have the 'for loop' because I used getElementsByClassName which gives a node list. I also created a codepen example:
http://codepen.io/anon/pen/dGqmMy
Instead of using complex string manipulation, use classList:
el[i].classList.add('openDiv');
I believe you might need to add a closure for the eventListeners to work.
So this would be considered as a solution:
var el = document.getElementsByClassName('applications');
var i;
for (i = 0; i < el.length; i++) {
(function (i) {
el[i].addEventListener("click", function() {
if (el[i]) {
el[i].classList.add('openDiv');
}
});
})(i);
}

Categories