i started learning jQuery and i wish to know why jquery methods cannot be applied
in this example :
$('p')[0].html('salut !'); //for this instruction i got this error :
//"TypeError: $(...)[0].html is not a function "
while this one works :
$('body')[0].tagName.toLowerCase();
i m confused and i wish to know what is the difference between these 2 cases.
here is another example for the same problem :
var listItems = $( 'li' );
var rawListItem = listItems[0]; // or listItems.get( 0 )
var html = rawListItem.html();
// Object #<HTMLInputElement> has no method 'html'
here is the way on how to use jquery methods by using the .eq() :
var listItems = $( 'li' );
var secondListItem = listItems.eq( 1 );
secondListItem.remove();
thanks for supplying some explanation for this.
A jQuery object is completely different to a
DOMElement.html(),val(),eq(), etc.DOMElement‘s… don’t. As they’reDOMElements, not jQuery objects.If you check the documentation for
.get(), you’ll see you get aDOMElementback, not a jQuery object. The same goes for[0]etc.eq(), however, returns a jQuery object which lets you do jQuery operations on them.tagNameis aDOMElementattribute, which is why can perform it on theDOMElements returned byget(), but can’t perform it on the jQuery objects returned byeq(). The opposite applies forhtml()when used onget()andeq().You can, of course, wrap any
DOMElementin a jQuery object via$(), which’ll let you perform jQuery operations on it;But in your situations, you should be using
eq():