Here I am trying to display multiples of 9 in one side and remaining in another side from 1 to 1000 numbers. but only 9 multiples are displaying how can i display remaining numbers on another side?
var i; for (i=1;i<=1000;i++){ if (i % 9 == 0) { document.writeln(i); document.write("<br>"); //document.write(i); } /*else if (i % 9){ document.write(i); document.write("<br>"); }*/ }
You can create two <ul>
elements, concatenate "<li>" + i + "</li>"
to appropriate ul
to index of ul
returned by .querySelectorAll()
var ul = document.querySelectorAll("ul"); var i; for (i = 1; i <= 1000; i++) { ul[i % 9 == 0 ? 0 : 1].innerHTML += "<li>" + i + "</li>"; }
ul { display: inline-block; position: absolute; width: 125px; } ul:nth-of-type(2) { left: 250px; }
<ul><li>multiples of 9</li></ul><ul><li>not multiples of 9</li></ul>
Yes, you can do that by first adding two div in HTML, And then simply add the the value of i in div like if i % 9 == 0 then we add this value to first div else we add this value to second div using simple javascript code. You can access first and second div by using document.getElementById("div_id");
<style>
.box{
width: 25%;
float: left;
}
</style>
<div id="div1" class="box"></div>
<div id="div2" class="box"></div>
<script>
var i;
var div1_output = "", div2_output = "";
var div1 = document.getElementById("div1");
var div2 = document.getElementById("div2");
for (i = 1; i <= 1000; i++) {
if (i % 9 == 0) {
div1_output += i + "<br>";
}
else if (i % 9){
div2_output += i + "<br>";
}
}
div1.innerHTML = div1_output;
div2.innerHTML = div2_output;
</script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.