EmmyS
EmmyS

Reputation: 12148

How can I get the value of an object property in jquery?

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?

Upvotes: 1

Views: 112

Answers (3)

zzzzBov
zzzzBov

Reputation: 179264

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

Upvotes: 1

Ricardo Lohmann
Ricardo Lohmann

Reputation: 26320

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

Upvotes: 1

Adil
Adil

Reputation: 148180

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;

Upvotes: 3

Related Questions