Ternary operator problem

I am trying to put a ternary operator inside an input element.The problem is that this causes the input element NOT to be printed in the browser.This is it:

 '<input class="price_show"'+  show_pr_val.value==1 ? "checked":"";+ 'type="radio" name="form[0][price_show]" value="1">yes'+
                        

Do you see anything wrong with the above,from a syntax point of view or any other view?

This is the whole code segment:here

You forgot to add a space after class and before type.

As well, you also need to alter this section of code:

// before
+  show_pr_val.value==1 ? "checked":"";+

// after
+  (show_pr_val.value==1 ? "checked":"") +

The semicolon was prematurely ending the statement, and wrapping the ternary statement in parentheses will ensure that it’s the result of the expression that gets concatenated with the rest of the string.

1 Like

thx…

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.