JQuery selector not working?

Say I have a few

<p> Hello </p> <p> Hello </p> <p> Hello </p> <p> Hello </p> 

I want to replace one "Hello" with "Good bye".

 So => $('p')[2].html('Good Bye'); 

Doesn't work why? Why should I use eq: selector?

It works when I do it.

 $('p:eq(3)').html('Good bye') 
+4
source share
4 answers

You are trying to use the html() with javascript object method that is supposed to be used with a jQuery object, you can use innerHTML with a javascript object so you need it,

Live demo

 $('p')[2].innerHTML = 'Good Bye'; 
+4
source

When you use parentheses, you are not getting the jQuery object, you are getting the original javascript object, for example if you want to use the javascript submit function from the form you should use

 jQuery('form')[0].submit() 
+2
source

The js object has no html () method, only the JQ object has.

you can do it like this:

 $('p').eq(2).html('hello,I am Chinese'); 
+2
source

Or you could use:

 $( $('p')[2]).html('Good Bye'); 
+1
source

All Articles