lundi 2 mai 2016

Javascript - if somethin then render

I'm trying to render a select list from JSON. It works fine, but when I try to add if to get the selected it fails. IF the selected ID equals a predefined number then write select=select to the array.

The error message I get is "Unexpected token if"

    var xmlhttp = new XMLHttpRequest();
var url = "roller.json";

xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        myFunction(xmlhttp.responseText);
    }
}
xmlhttp.open("GET", url, true);
xmlhttp.send();

function myFunction(response) {
    defRolle = 6;
    solSelect ="selected=selected";
    var arr = JSON.parse(response);
    var i;
    var out = "<select>";

    for(i = 0; i < arr.length; i++) {
        out += "<option value='"+
        arr[i].RolleID +
        "'" + if (arr[i].RolleID===defRolle) {return solSelect} +
        " name='"+
        arr[i].RolleNavn +
        "'>" +
        arr[i].RolleNavn +
        "</option>";
    }
    out += "</Select>";
    document.getElementById("endreRolle").innerHTML = out;
}

I appreciate all the help I can get.

Aucun commentaire:

Enregistrer un commentaire