1

I have a group of objects on my page. I want to get the first one, so I do this:

var tmpLi = li.first();
console.dir(tmpLi);

This works fine; it returns this in firebug:

enter image description here

I need to return the value of the outerHTML element, but can't seem to figure out how to get it. I've tried:

var tmpLi = li.first().data("outerHTML");

and

var tmpLi = li.first().attr("outerHTML");

both of which return "undefined". Help?

3 Answers 3

3

You need DOM object for outerHTML instead of jQuery object so convert it to DOM object to access outerHTML property.

Live Demo

var tmpLi = li.first()[0].outerHTML;
Sign up to request clarification or add additional context in comments.

2 Comments

I tried, this, and it printed out "There are no child objects".
Oops, never mind. Needed to change from console.dir to console.log and now it works.
1

It returns an object, so you have to access it like the following.
li.first()[0].outerHTML

Comments

1

You either need to use the prop method, which gets the underlying object's property value:

li.first().prop('outerHTML');

...or unwrap the DOM object from the jQuery selection:

li[0].outerHTML

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.