How to concatenate if condition in with jquery html?

Viewed 235

I am using jQuery code to make html in front end, but i am getting error in if condition unexpected token if. please help me to fix this problem, i guess there is problem in concatenate jQuery with HTML. Here is my code below.

innerapptemp.blade.php


    for (iS = 0; iS < response.data.stocks.length; ++iS) {
                            stocks += '<tr class="table-row"><td><select> <option> Select </option> <option value="1"' +if(response.data.stocks[iS].color_tag==1) +' selected> Interested </option> <option value="1"' +if(response.data.stocks[iS].color_tag==2) + ' selected> Not Interested </option> </select></td><td>' + response.data.stocks[iS].scan_name + '</td><td>' + response.data.stocks[iS].name + '</td><td>' + response.data.details[response.data.stocks[iS].name].close_price + '</td><td>' + response.data.details[response.data.stocks[iS].name].open_price + '</td><td>' + response.data.details[response.data.stocks[iS].name].last_price + '</td><td>' + response.data.details[response.data.stocks[iS].name].volume + '</td><td>' + response.data.details[response.data.stocks[iS].name].change_from_open + '</td><td>' + response.data.details[response.data.stocks[iS].name].pivot_point + '</td><td>' + response.data.details[response.data.stocks[iS].name].r3 + '</td><td>' + response.data.details[response.data.stocks[iS].name].changefromr3 + '</td><td><button class="btn btn-primary place-btn" attr-order_type="MARKET" attr-product="NRML" attr-symble="{{ $stock->name }}" attr-id="{{ $stock->name }}" data-toggle="modal" data-target="#place-order">Place Order</button></td><td><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#depthMain">click</button></td></tr>';
                        }

1 Answers

First, lets format your code so you can actually see it:

for (iS = 0; iS < response.data.stocks.length; ++iS) {
    stocks += '<tr class="table-row"><td><select> <option> Select </option> <option value="1"' +
    if(response.data.stocks[iS].color_tag==1) +
    ' selected> Interested </option> <option value="1"' +
    if(response.data.stocks[iS].color_tag==2) +
     ' selected> Not Interested </option> </select></td><td>' +
     response.data.stocks[iS].scan_name +
     '</td><td>' +
     response.data.stocks[iS].name +
     '</td><td>' +
     response.data.details[response.data.stocks[iS].name].close_price +
     '</td><td>' +
     response.data.details[response.data.stocks[iS].name].open_price +
     '</td><td>' +
     response.data.details[response.data.stocks[iS].name].last_price +
     '</td><td>' +
     response.data.details[response.data.stocks[iS].name].volume +
     '</td><td>' +
     response.data.details[response.data.stocks[iS].name].change_from_open +
     '</td><td>' +
     response.data.details[response.data.stocks[iS].name].pivot_point +
     '</td><td>' +
     response.data.details[response.data.stocks[iS].name].r3 +
     '</td><td>' +
     response.data.details[response.data.stocks[iS].name].changefromr3 +
     '</td><td><button class="btn btn-primary place-btn" attr-order_type="MARKET" attr-product="NRML" attr-symble="{{ $stock->name }}" attr-id="{{ $stock->name }}" data-toggle="modal" data-target="#place-order">Place Order</button></td><td><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#depthMain">click</button></td></tr>';
}

You can't have a long string and concatenate (+) if statements. You need to resolve those first and include them in the string as variables, or break it up and have them in between the string you're trying to make.

For example, this part of the string:

 <option value="1"' 
        +if(response.data.stocks[iS].color_tag==1) 
        +' selected> Interested </option> <option value="1"' 
        +if(response.data.stocks[iS].color_tag==2) 
        + ' selected> Not Interested </option> 

needs to be refactored like this:

// this part before you start the big long string
opt1selected = opt2selected =''; 
if(response.data.stocks[iS].color_tag==1) opt1selected=' selected="selected"';
if(response.data.stocks[iS].color_tag==2) opt2selected=' selected="selected"';

   // then in the string
   // .... 
   '<option value="1"' +opt1selected+' </option>' +
    '<option value="2"' +opt2selected+' </option>' +
    

      
Related