How to make this javascript in a loop?

Sorry, I'm new to Javascript. I do not know how to make this loop in Javascript. This is my fiddle , which is so ugly because there is no loop there. I did this in PHP, but I don't know how to make such a loop in javascript. This is my code in PHP:

<?php
    $username='michel jackson';
    for($a=1;$a<=2;$a++){
        echo "
            <div id='bigbox'>
        ";
        for($b=1;$b<=$a;$b++){
            echo "
                <div class='colkiri'>
                    <div class='colkirichild'></div>
                </div>
            ";
        }
        echo "
            <div class='colkanan'>
                    <div class='colkananchild'>
                        <div id='username'>
                            <span class='usernamechild'><img src='http://images2.fanpop.com/images/photos/7600000/Bad-michael-jackson-7647469-1787-2560.jpg' width='15' height='15' class='bayangan'></img> $username</span> <span class='countdown'>5 minutes ago</span>
                        </div>
                        <div id='comment'>
                            <div class='commentchild'>Michael Joseph Jackson was born on August 29, 1958. He was the eighth of ten children in an African-American working-class family who lived in a two-bedroom house on Jackson Street in Gary, Indiana, an industrial city and a part of the Chicago metropolitan area.[12][13] His mother, Katherine Esther Scruse, was a devout Jehovah Witness.</div>
                            <div id='reportthis'>
                                <span id='idreply' onclick='clickreply(this)'>reply</span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        ";
    }
?>

I am trying to convert to javascript as fiddle , but it is not working yet. I don't know how to use a loop in Javascript. Thank.

this is my javasript code that i don't do:

var c=[1,2];
var z,y,x,w,a,b;
function mj1(){
    mj2();
    document.getElementById("showup").innerHTML='????';//<- which one? i don't know how to choose the variable 
}
function mj2(){
    username='michel jackson';
    for(a=1;a<=c.length;a++){
        z="<div id='bigbox'>"+
        for(b=1;b<=c.length;b++){
            y[b]="<div class='colkiri'>"+
                "<div class='colkirichild'></div>"+
            "</div>";
        }
        x="<div class='colkanan'>"+
                "<div class='colkananchild'>"+
                    "<div id='username'>"+
                        "<span class='usernamechild'><img src='http://images2.fanpop.com/images/photos/7600000/Bad-michael-jackson-7647469-1787-2560.jpg' width='15' height='15' class='bayangan'></img> username</span> <span class='countdown'>5 minutes ago</span>"+
                    "</div>"+
                    "<div id='comment'>"+
                        "<div class='commentchild'>Michael Joseph Jackson was born on August 29, 1958. He was the eighth of ten children in an African-American working-class family who lived in a two-bedroom house on Jackson Street in Gary, Indiana, an industrial city and a part of the Chicago metropolitan area.[12][13] His mother, Katherine Esther Scruse, was a devout Jehovah Witness.</div>"+
                        "<div id='reportthis'>"+
                            "<span id='idreply' onclick='clickreply(this)'>reply</span>"+
                        "</div>"+
                    "</div>"+
                "</div>"+
            "</div>"+
        "</div>";
    }
}
-5
source share
2 answers

This is your javascript PHP loop. Just the same ... Remember to use the tag <script> javascript code here </script>to tell the browser that it is javascript

for (a = 1; a < 2; a++) { 
    //Your stuff inside here
}

HTML div, :

document.getElementById('divID').innerHTML = '<p>your data here</p>';

, JQuery, :

$('#divID').append('<p> your data here </p>');
+2

Javascript:

for(var a = 1; a <= 2; a++){
        // do stuff
        for(var b = 1; b <= a; b++){
            // do stuff
        }
//do stuff
}
+1

Source: https://habr.com/ru/post/1599211/


All Articles