-2

I have two loop statement like this

<input type="button" value="Test1" onclick="realopen()"/>
<br/>
<div class="test1">
<p id="outside"></p>
<br />
</div>
<div class="test2">
<p id="outred"></p>
<br />

and code is :

function realopen() {
    var hell = document.getElementsByClassName('test1')[0].childNodes;
    var twel = document.querySelectorAll("id=[outside]");
    var solo = document.getElementsByClassName('test2')[0].childNodes;
    var seap = document.querySelectorAll("id=[outred]");
    for (var i = 0; i < hell.length; i++) {
        setInterval(function () {
            for (var j = 0; j < twel.length; j++) {
                twel[j].innerHTML = 999;
            }
        }, 10000);
    }
    for (var x = 0; x < solo.length; x++) {
        setInterval(function () {
            for (var y = 0; y < seap.length; y++) {
                seap[y].innerHTML = 1234;
            }
        }, 10000);
    }
}

here , why in result we cannot get answer from both of loop statements ? result show only second loop statement content for me .

Dani
  • 33
  • 1
  • 6
  • 1
    What is the purpose of looping through `hell` and `solo`? Don't `#test1` and `#test2` both have only one child node? So `length === 1`? If that's the case, why loop? What am I missing? – alexanderbird May 02 '17 at 21:16
  • Also, it looks to me like `twel.length` and `seap.length` are undefined... am I wrong? – alexanderbird May 02 '17 at 21:19
  • i dont need loop exactly , i need to show result at the same time – Dani May 02 '17 at 21:20
  • So you want the text content of `#outside` and `#outred` to be set to `999` and `1234` at the same time, 10 seconds after running `realopen`? – alexanderbird May 02 '17 at 21:22
  • yes , exactly , my code is incomplete – Dani May 02 '17 at 21:24
  • Possible duplicate of [JavaScript closure inside loops – simple practical example](http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example) – Heretic Monkey May 02 '17 at 21:41
  • @MikeMcCaughan that was my first thought too, but that's not the issue here. The outer loop runs once and the inner loop never runs. – alexanderbird May 02 '17 at 21:51
  • 1
    The whole question is unclear, and by the OP's own admission, incomplete. I don't really care how it gets closed, but it should get closed (or edited by the OP to include an [mcve]). – Heretic Monkey May 02 '17 at 21:56
  • @MikeMcCaughan again , i tested this code and edit it again , please read it again , check it in your browser , and say me last answer . thanks ahead – Dani May 02 '17 at 22:04
  • 1
    Honestly, you really need to do some research. The answer you have tells you to use `getElementById`, but you insist on using `querySelectorAll`. Since IDs must be unique to a document, you shouldn't use a method that returns more than one element. The syntax you've used for that [`querySelectorAll`](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll) call is also incorrect. The fact that you've edited this question 4 times tells me you're not trying different possibilities before asking. Please, read [ask]. – Heretic Monkey May 02 '17 at 22:11
  • @Mike McCaughan code is basically like this , and i want to get asnwer from different results , by the way i opened this question after several research , please pay attention to this we have id like outside and outside1 and outside2 and etc , and we should use querySelectorAll to get it , by the way as you know this code can get result from last loop statement , please check it in your browser and say me what can i do , thank you – Dani May 02 '17 at 22:16
  • Please pay attention to what my comments are saying, and read the pages I'm linking you to. – Heretic Monkey May 02 '17 at 22:20
  • @Mike McCaughan my answer is not over there – Dani May 02 '17 at 22:28
  • @Mike McCaughan over there we dont have any thing about several loop with querySelectorAll and get answer from both of them – Dani May 02 '17 at 22:37

1 Answers1

1

You're on the right track, except you made it much more complicated than it has to be. Just grab the elements and set their content.

function realopen() {
  var twel = document.getElementById("outside");
  var seap = document.getElementById("outred");
  setInterval(function() {
    twel.innerHTML = 999;
    seap.innerHTML = 1234;
  }, 10000);
}
<input type="button" value="Test1" onclick="realopen()" />
<br/>
<div class="test1">
  <p id="outside"></p>
  <br />
</div>
<div class="test2">
  <p id="outred"></p>
  <br />
alexanderbird
  • 3,847
  • 1
  • 26
  • 35
  • this is wrong , i need class certainly , other than without class this is wrong too – Dani May 02 '17 at 21:39
  • please read my question again , i edited with new method , please answer for my question with using class and querySelectorAll – Dani May 02 '17 at 21:49
  • If `twel` is an array of elements (which it would be if it's assigned the result of `querySelectorAll`), then you can iterate over it with a for loop and do the same as I've done here for each item in the loop. If you do that, then you should read [the link suggested by @MikeMcCaughan](http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example) – alexanderbird May 02 '17 at 21:55
  • again , i tested this code and edited again , please read it again , check it in your browser , and say me last answer . thanks ahead – Dani May 02 '17 at 22:07