0

My javascript code like this :

var res =  `<td>
                if(product.photo == photo.name)
                <div class="box-check">
                    <span class="fa fa-check"></span>
                </div>
            </td>`;

I try like that. But seems it's wrong

How can I do it?

0

1 Answer 1

4

simplest (easiest) change I can think of is as follows:

var inner = product.photo == photo.name ? `<div class="box-check">
       <span class="fa fa-check"></span>
    </div>` : '';
var res =  `<td>${inner}</td>`;

alternatively

var res = `<td>${product.photo == photo.name?'<div class="box-check"><span class="fa fa-check"></span></div>':''}</td>`;

but you can NOT (easily) make the <div><span etc multiline in this case

I didn't try hard enough - nested template literals FTW :p

var res = `<td>${product.photo == photo.name?`<div class="box-check">
    <span class="fa fa-check"></span>
</div>`:''}</td>`;
Sign up to request clarification or add additional context in comments.

3 Comments

"but you can NOT (easily) make the <div><span etc multiline in this case" - Why not? You can nest template literals inside ${...}. Or you could say `<td>` + ternary + `</td>` (with or without line breaks).
I could've sworn I did try nested templates - clearly a typo when I tried :p
@Jaromanda X, Seems you can help me. Look at this : stackoverflow.com/questions/44665670/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.