I am trying to get the html of "li" elements and alert them as a string.
HTML:
<ul>
<li>lorem ipsum dolor <span>sit</span> amet 1</li>
<li>lorem ipsum dolor <span>sit</span> amet 2</li>
<li>lorem ipsum dolor <span>sit</span> amet 3</li>
<li>lorem ipsum dolor <span>sit</span> amet 4</li>
<li>lorem ipsum dolor <span>sit</span> amet 5</li>
<li>lorem ipsum dolor <span>sit</span> amet 6</li>
</ul>
<p id="greeting">Hello <strong>World</strong>!</p>
Javascript:
$(document).ready(function(e){
var list = $("li");
var greeting = $("#greeting");
var content ="";
for(var i=0, len = list.length; i<len; i++) {
content += $(list[i]).html();
//why do I have to use the $ here instead of just doing list[i].html()
//content += list[i].html(); why does this not work?
}
alert(content);
alert(greeting.html()); //why does this work without the $ ?
});
I have done some research and understood that jQuery selectors return DOM elements wrapped in jQuery objects so that we can apply jQuery methods on it but why does the line greeting.html() work fine without the $?
TypeError: list[i].html is not a function
why do I get this error when I do list[i].html instead of $(list[i]).html()?
Here is the fiddle.