Reputation: 11851
I have this append statement
$("#questions").append($("<tr><td>" + if(value.NA == true) { + "<input type='checkbox' id='" + value.Question_ID + "-na' class='na' /> " + } + "</td>");
I am trying to add a condition to the append, but I get this error:
Uncaught SyntaxError: Unexpected token if
I am assuming this if is illegal, what would be the best way to accomplish what I am trying to accomplish?
Upvotes: 0
Views: 3686
Reputation: 5201
You could use Javascript conditional (ternary) operator:
$("#questions").append($("<tr><td>" + (value.NA ? "<input type='checkbox' id='" + value.Question_ID + "-na' class='na' /> " : "") + "</td>");
Upvotes: 1
Reputation: 87203
if
cannot be used within strings.
Use a variable to store the HTML and use the variable in the append
.
var input = ''; // Set as empty string
if(value.NA === true) {
input = "<input type='checkbox' id='" + value.Question_ID + "-na' class='na' /> ";
}
$("#questions").append($("<tr><td>" + input + "</td>"));
Ternary operator(?..:
) can also be used
$("#questions").append($("<tr><td>" + (value.NA ? "<input type='checkbox' id='" + value.Question_ID + "-na' class='na' /> " : '') + "</td>"));
Upvotes: 6