user1050619
user1050619

Reputation: 20856

html() not working on chrome browser

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.

http://jsfiddle.net/7RCyX/

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

Answers (3)

Raunak Kathuria
Raunak Kathuria

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

enter image description here

you can change it to

var firstitem = listitems.eq(1); // or use nth:child selector 

http://jsfiddle.net/7RCyX/4/

Upvotes: 2

Chris Bornhoft
Chris Bornhoft

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

Onur Topal
Onur Topal

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

Related Questions