I have created a autocomplete suggestion box which is working fine but the problem is when I use if and else
and console.log()
it shows an error in my console ie Uncaught SyntaxError: Unexpected token if
I don't know why? How can I solve this issue? Please help me.
code:
$("#tag").tokenInput("<?php echo base_url(); ?>search_tag", {
onResult: function(results){
$.each(results, function (index, value) {
value.name = if(value.client_img==''){ "<img src='<?php echo base_url(); ?>assets/user.png'>" }else{ "<img src='<?php echo base_url(); ?>assets/client_img/"+value.client_img+"'>" }+value.fname+" "+value.lname;
});
//return results;
console.log(results);
}
});
The if
should be earlier so that the results of the if/ else block are applied to the value.name property.
$.each(results, function (index, value) {
if(value.client_img==''){
value.name ="<img src='<?php echo base_url(); ?>assets/user.png'>"
}else{
value.name = "<img src='<?php echo base_url(); ?>assets/client_img/"+value.client_img+"'>" +value.fname+" "+value.lname;
}
});
or use a ternary operator
$.each(results, function (index, value) {
value.client_img==''
? value.name ="<img src='<?php echo base_url(); ?>assets/user.png'>"
: value.name = "<img src='<?php echo base_url(); ?>assets/client_img/"+value.client_img+"'>" +value.fname+" "+value.lname;
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.