The condition does not work in javascript

I check the condition here to select a value from the drop-down list. But the next line doesn't work, it seems.

<option value="Chairman"'+ val["designation"]== "Chairmain" ? "selected" : "" +'>Chairman</option>

I think there is a problem with 'and ". I tried many ways to fix this, but that didn't help.

Below is my complete code that I am using:

$.each(management_details,function(key,val){
    cnt++;
    htm += '<select name="management[]" class="repeat-select-management">\n\
        <option value="0">Select Management</option>\n\
        <option value="Chairman"'+ val["designation"]== "Chairmain" ? "selected" : "" +'>Chairman</option>\n\
        <option value="Vice Chairman">Vice Chairman</option>\n\
        <option value="Vice Chancellor">Vice Chancellor</option>\n\
        <option value="Secretary">Secretary</option>\n\
        <option value="Registrar">Registrar</option>\n\
        <option value="Dean">Dean</option>\n\
        <option value="Owner">Owner</option>\n\
        <option value="Other">Other</option>\n\
    </select>';
});

Can someone tell me how to fix this?

+4
source share
1 answer

Adding a paragraph around a three-dimensional statement should do the trick.

Try replacing this line:

'<option value="Chairman"'+
    val["designation"]== "Chairmain" ? "selected" : "" +
    '>Chairman</option>\n\'

WITH

'<option value="Chairman"'+
    (val["designation"]== "Chairmain" ? "selected" : "") +
    '>Chairman</option>\n\ '

JavaScript was probably confused with what is part of the elsetriple operator and what is not.

( , )

+5

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


All Articles