Nanda
Nanda

Reputation: 173

How to add condition before adding rows to the table: jquery

I have an html template which consists of a table and an add button. When the add button is clicked, selections are added to the table. I want to add a condition that if one of the selections contains "Map" it should add modal to that particular column. For example:

<div class="reports">
    <div class="panel">
        <table class="A">
            <thead>
                <td>A</td>
                <td>B</td>
                <td>C</td>
            </thead>
            <tbody>
            </tbody>
        </table>
   </div>
   <button class="add" type="button">Add</button>
</div>

jQuery script is:

$('.reports').on('click','.add',function(){
  $(this).find('table').append(table rows);// how to add condition inside append 
});

There are three values for three columns: value1, value2, value3:

if text2=="Map"{ 
    value2 = "modal-dialog"; 
} else { 
    value2 = text2; 
}

How do I add an if condition inside the append()?

Upvotes: 1

Views: 2027

Answers (2)

Gautam
Gautam

Reputation: 255

Instead of adding condition inside append(). Try outside and call that element in the append(). Example:

$('.reports').on('click','.add',function(){
  if (text2 == map_text) {
            var map_elements += '<button role="button" data-target="#map-id" class="map-btn btn btn-primary" data-toggle="modal">Map</button></span>';
        } else {
            var map_elements += text2;
        }
 $(this)
         .find('table')
          .append('<tr><td>'+map_elements+'</td></tr>'); //this should work
});

Upvotes: 2

Bryan Mudge
Bryan Mudge

Reputation: 432

Use

    $('.reports').on('click','.add',function(){
      if text2=="Map"{ 
    do something
} else { 
   do something else
}


    });

Upvotes: 1

Related Questions