ddeadlink
ddeadlink

Reputation: 247

innerHTML is not working in jquery

i've got nodes collection $('.text-block'). And i need to insert some html in it. doing like this

$('.text-block')[0].html('example text') 

or

$('.text-block').get(0).html('example text') 

and i've got an error

html is not a function

What's wrong with this jquery

Upvotes: -1

Views: 438

Answers (2)

PHP Geek
PHP Geek

Reputation: 4033

You can use first keyword with the class name to change its inner html

$('.text-block:first').html('example text');

Upvotes: 0

Pranav C Balan
Pranav C Balan

Reputation: 115222

Both the get() method and [] (index) return DOM object, so you can't use jQuery methods on it. Use the eq() method to get a jQuery object based on an index, or update the innerHTML property of DOM object:

$('.text-block').eq(0).html('example text') 
// or
$('.text-block')[0].innerHTML = 'example text';

$('.text-block').eq(0).html('example text 1')
$('.text-block')[1].innerHTML = 'example text 2';
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div>
  <div class="text-block"></div>
  <div class="text-block"></div>
</div>

</html>

Upvotes: 4

Related Questions