Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the content in the td tag using jquery

I have a table defined like

<table>
  <tr>
    <td>
       <input type='text' />
    </td>
    <td>
      <button id ='first'>Button</button>
    </td>
    <td>
      <input type='text' />
   </td>
 </tr>

$("#first").live('click',function(){
    $(this).prev().remove();
    $(this).remove();
});

How can i remove the previous element in the td. But i don't want to remove the td

like image 261
Jonathan Avatar asked Sep 03 '25 06:09

Jonathan


2 Answers

If you want to remove the complete contents of that previous TD (rather than a specific INPUT element), this will work:

$(document).on("click", "#first", function() {
    $(this).closest("td").prev().empty();
});
like image 91
Brian Stephens Avatar answered Sep 04 '25 23:09

Brian Stephens


http://jsfiddle.net/sfHBn/1/

$(document).on("click", "#first", function() {
    $(this).closest("td").prev().html("");
});

This will remove all content in previous td tag.

like image 30
Miljan Puzović Avatar answered Sep 04 '25 23:09

Miljan Puzović