Q:

How can I add a condition on the way of .append()?

Here is my code:
$.each(cities.cities, function (i, item) {
    $('select#city').append($('<option>', {
        value: item.id,
        text : item.name
    }));
});

Now I need to add a condition on the way of appending. I mean I want to add selected attribute to that option which has the same item.id as city_id variable. How can I do that?

A:

selected is boolean property - which can be the result of a conditional:
$.each(cities.cities, function (i, item) {
    $('select#city').append($('<option>', {
        value: item.id,
        text : item.name,
        selected: item.id === city_id
    }));
});

