jeudi 18 juillet 2019

how to escape string and do some (if) logic in javascript?

I have a code like this :

if (tmp_loket==0) {
    for (var i = 1; i<= data['jumlah_loket']; i++) {
        loket = '<div class="col-md-3" style="width: 32%;">'+
            '<div class="'+ i +
                    ' jumbotron" style="padding: 0px;">'+
                '<button class="btn btn-danger" type="button" style="width: 100%;">'+ if(i==1){ 'UMUM' } +'</button>'+
                '<h1> '+data["init_counter"][i]+' </h1>'+
                '<button class="btn btn-danger" type="button" style="width: 100%;"><span class="glyphicon glyphicon-credit-card">&nbsp;</span>LOKET '+ i +'</button>'+
                '</div>'+
            '</div>';
        $(".loket").append(loket);
    }

    tmp_loket = data['jumlah_loket'];
}

I want when looping is executed, value i = 1 then what appears is "UMUM", when value i = 2 then what appears is "ANAK".

But how to escape this single quote and do (if) logic?

here is the problem :

'<button class="btn btn-danger" type="button" style="width: 100%;">'+ if(i==1){ 'UMUM' } +'</button>'+

Thank you.

Aucun commentaire:

Enregistrer un commentaire