I'm using a jQuery function to build an html table based on some json I have available. I use a for loop
to build tables for each entry in my json. In one case I want to check the value of the json element to see if it has a value. If so I want to build an html link with an image. I thought using a ternary statement would be the cleanest by checking for the value using .isEmptyObject()
function. When I do it doesn't build as expected and acts like it just gives up on that line all together. Here's what I've tried.
'<td> '
+ (!jQuery.isEmptyObject(json[i].pdf_files)) ?
' <a href="http://site.example/assets/_images/products/pdf/' +
json[i].pdf_files + '" target="_blank"> <img src="img/pdf-32.png" alt="pdf" /> Spec Sheet</a>' : '' +
' </td>' + //continue with appending rest of table
If I just do this it works:
'<td> ' + ' <a href="http://site.example/assets/_images/products/pdf/' + json[i].pdf_files + '" target="_blank"> <img src="img/pdf-32.png" alt="pdf" /> Spec Sheet</a> </td>' +
答案 0 :(得分:1)
So add parenthesis around the entire ternary operator so it know where to stop.
'<td> ' + ( x ? 'foo' : 'bar' ) + '</td>'