jquery syntax error

7

Hello, I'm having a problem with using this code. I want to find the following code in the div with class output: I used append, here is giving syntax error, could you help me?

>
 $('.output').append('<li class="canal row"><div class="col"><img style="width: 80px; height: 80px;" class="rounded-circle" src="'+data2.logo+'"></div> <div class="col-6"><p class="tituloCanal"><a href="'+data2.url+'" class="linkTitulo">'+data2.display_name+'</a> </p><p class="desc">'+data2.status+'</p> </div>'+
 if(data.stream===null){+'<div class="online col"> <a href="#" class="onlineIcon">on</a> </div>'+} else{'+<div class="online col"> <a href="#" class="onlineIcon">on</a> </div>'+}+'</li>');
    
asked by anonymous 05.06.2017 / 21:11

2 answers

5

You can not use if/else within string concatenation in this way.

Separate the if / else part and concatenates only the result. Something like this:

var stream = data.stream === null ?
  '<div class="online col"> <a href="#" class="onlineIcon">on</a> </div>' :
  '<div class="online col"> <a href="#" class="onlineIcon">on</a> </div>';

$('.output').append(
  '<li class="canal row"><div class="col"><img style="width: 80px; height: 80px;" class="rounded-circle" src="' +
  data2.logo + '"></div> <div class="col-6"><p class="tituloCanal"><a href="' +
  data2.url + '" class="linkTitulo">' +
  data2.display_name + '</a> </p><p class="desc">' +
  data2.status + '</p> </div>' + 
  stream +
  '</li>'
);
    
05.06.2017 / 21:15
5

You can not use if this way, you need a ternary operator.

See the example below. If the condition is true it will be concatenated what comes after the ? , otherwise it will be concatenated what comes after : .

Obviously you could organize the code better and deal with it, moreover, a tip: give a better organized code.

Anyway, the problem is to use this if-else within the concatenation.

(data.stream === null
? '<div class="online col"> <a href="#" class="onlineIcon">on</a> </div>' 
: '<div class="online col"> <a href="#" class="onlineIcon">on</a> </div>' ) 
+ '</li>';
    
05.06.2017 / 21:14