Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery changing content of table cell

OK, this is an embarrassingly simple question. Why doesn't the following jQuery example work? Obviously it is supposed to change the 'a' in the table to 'hello'.

HTML code:

    <table id='table1'>
      <tr>
          <td>a</td>
          <td>b</td>
      </tr>
    </table>​

JavaScript (JQuery) code:

    $("#table1 td:contains('a')").innerHTML="hello";
like image 741
user1069609 Avatar asked Feb 23 '12 10:02

user1069609


2 Answers

use the html function like this

 $("#table1 td:contains('a')").html("hallo");

if you want use innerHTML (is a DOM method, not a Jquery Method), you have to select the DOMElement first.

jQuery(document).ready(function(){
    $("#table1 td:contains('a')").each(function(){
    jQuery(this)[0].innerHTML = "Hallo";
    });
});
like image 178
silly Avatar answered Oct 12 '22 23:10

silly


It doesn't work because innertHTML is a property of a DOM element and not of the jQuery object. You want

$("#table1 td:contains('a')").html("hello");  
like image 32
Nicola Peluchetti Avatar answered Oct 12 '22 23:10

Nicola Peluchetti