Reputation: 20856
Im trying to use innerhtml and html() but html() does not work on the browser.
what could be the reason...
when I replace innerHTML to html() in the fiddle, I dont get any result.
html
<ul>
<li>First</li>
<li>Second</li>
</ul>
jquery:
var listitems = $('li')
var firstitem = listitems[1]
var x = firstitem.innerHTML
alert(x)
Upvotes: 0
Views: 125
Reputation: 3225
var firstitem = listitems[1]
by specifying this it means it will give you back dom element (see image), so you can access properties of object like li.className
you can change it to
var firstitem = listitems.eq(1); // or use nth:child selector
Upvotes: 2
Reputation: 4291
x
is not being set since firstitem is only the li
element. You need to add $()
.
var listitems = $('li');
var firstitem = listitems[1];
var x = $(firstitem).html();
alert(x);
The above code works as intended.
Upvotes: 0
Reputation: 3061
when you html()
does not work do you mean firstitem.html()
, it never do that because it s html node not jQuery object to convert it to jQuery try $(firstitem)
or $("li:nth-child(2)")
.
Upvotes: 0