JQuery changing content of table cell
Asked Answered
P

2

5

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";
Personally answered 23/2, 2012 at 10:45 Comment(0)
A
9

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";
    });
});
Alleris answered 23/2, 2012 at 10:46 Comment(1)
@Personally look the second part in my answerAlleris
C
4

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");  
Claudetteclaudia answered 23/2, 2012 at 10:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.