1

I have a javascript array with a list of informations about people. I populate this array dynamically into a table on the frontend.

But, not every person has a website. So I want to enter a condition for the website e.g. if(url !== "") {<a href="'+ list[i].url + '">Webseite</a>}

But it just tells me "unexpected token if". Ideas?

for(var i = 0; i < list.length; ++i ) {
   $(".list").append('\
     <tr>\
     <td>'+ list[i].name + '</td>\
     <td>' + list[i].area1 + ',<br> ' + list[i].area2 + ',<br> ' + list[i].area3 + '</td>\
     <td>'+ list[i].small_area + '</td>\
     <td>'+ list[i].studies + '</td>\
     <td>'+ list[i].email + '</td>\
     <td><a href="'+ list[i].url + '">Webseite</a></td>\
     <td>'+ list[i].other + '</td>\
     </tr>\
   ');
 };

4 Answers 4

1

You can't use if condition inside append instead you can use a ternary operator like this

for (var i = 0; i < list.length; ++i) {
  $(".list").append('\
    <tr>\
    <td>' + list[i].name + '</td>\
    <td>' + list[i].area1 + ',<br> ' + list[i].area2 + ',<br> ' + list[i].area3 + '</td>\
    <td>' + list[i].small_area + '</td>\
    <td>' + list[i].studies + '</td>\
    <td>' + list[i].email + '</td>\
    <td>' + (list[i].url != null ? '<a href="' + list[i].url + '">Webseite</a>' : '') + '</td>\
    <td>' + list[i].other + '</td>\
    </tr>\
    ');

};
Sign up to request clarification or add additional context in comments.

1 Comment

hmm it doesnt work for me... it says missing ) after argument list
0

You can prepare a variable called Webseite before the append and then add it to the rest. In one case it's the url in the other it's empty.

 var list = [
 {name:"One",url:'http://google.com'},{name:"Two", url:""}
  
 ];
 
 for(var i = 0; i < list.length; ++i ) {
     
     
     if(list[i].url !== "") {
        var Webseite = '<a href="'+ list[i].url + '">Webseite</a>'
     }else{
        var Webseite = '';
     } 
     
     $(".list").append('\
    <tr>\
    <td>'+ list[i].name + '</td>\
    <td>' + list[i].area1 + ',<br> ' + list[i].area2 + ',<br> ' + list[i].area3 + '</td>\
    <td>'+ list[i].small_area + '</td>\
    <td>'+ list[i].studies + '</td>\
    <td>'+ list[i].email + '</td>\
    <td>' + Webseite + '</td>\
    <td>'+ list[i].other + '</td>\
    </tr>\
    ');
 }
td{
padding:10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="list"></div>

1 Comment

Glad I could help. Please feel free to mark it as answered.
0

Try this

(list[i].url) ? list[i].url : ''

This is a shorthand if..else using conditional (ternary) operator

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

Comments

0

You can create a string as a result and checking each time if the element of the object exists then add it.

for(var i = 0; i < list.length; ++i ) {
    var result = '<tr>'
    result += '<td>'+ list[i].name + '</td>';
    result += '<td>' + list[i].area1 + ',<br> ' + list[i].area2 + ',<br> ' + list[i].area3 + '</td>';
    result += '<td>'+ list[i].small_area + '</td>';
    result += '<td>'+ list[i].studies + '</td>';
    result += '<td>'+ list[i].email + '</td>';
    if (list[i].url) {
      result += '<td><a href="'+ list[i].url + '">Webseite</a></td>';
    }
    result += '<td>'+ list[i].other + '</td>';
    result += '</tr>';
    $(".list").append(result);
  };

Comments

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.