JQuery modifies table cell contents

Ok, this is an awkwardly simple question. Why doesn't the following jQuery example work? Obviously, it is assumed that β€œa” in the table will be β€œhello”.

HTML code:

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

JavaScript Code (jQuery):

  $("#table1 td:contains('a')").innerHTML="hello"; 
+4
source share
2 answers

use html function like this

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

if you want to use innerHTML (this is the DOM method, not the JQuery Method), you need to select DOMElement first.

 jQuery(document).ready(function(){ $("#table1 td:contains('a')").each(function(){ jQuery(this)[0].innerHTML = "Hallo"; }); }); 
+7
source

This does not work because innertHTML is a property of the DOM element, not the jQuery object. Do you want to

 $("#table1 td:contains('a')").html("hello"); 
+3
source

Source: https://habr.com/ru/post/1398006/


All Articles