Question User Question User - 1 year ago 39
jQuery Question

how to use append and after using if condition in jquery

How to simplify this code using jquery

FIddle

$('#tableclick').click(function(){

if($('#tr_second').length == 0){ var adppend =$('#append_tr').append; } else { var adppend =$('#tr_second').append; }

adppend('<tr><td>test1</td><td>test2</td><td>test3</td></tr>');
});

Answer Source

This way you can use your condition. Here's a jsFiddle

$('#tableclick').click(function() {
  var html = '<tr><td>test1</td><td>test2</td><td>test3</td></tr>';
  if (!$('#tr_second').length) {
    $('#append_tr').append(html);
  } else {
    $(html).insertAfter($('#tr_second'));
  }
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download