4

I have something like this

<ul>
<li class="aclass" id="a">content</li>
<li class="aclass" id="b">content</li>
<li class="aclass" id="c">content</li>
<li class="aclass" id="d">content</li>
<li class="aclass" id="e">content</li>
<li class="aclass" id="f">content</li>
</ul>

I have code like

$(".aclass").live("mousedown",function() {

alert($this.html());

});

This will alert the content, what I would like to do is alert the entire element like

<li class="aclass" id="f">content</li>

I've tried $(this).parent() but that returns the whole UL

1

3 Answers 3

6
alert($(this).clone().wrap('<div/>').parent().html());
Sign up to request clarification or add additional context in comments.

2 Comments

but...you will end up with invalid markup. Should clone the element first
@redsquare, very good point. I've updated my answer to take your suggestion into account.
0
alert($('<div/>').append($(this).clone()).html());

http://jsfiddle.net/keegan3d/nfEyQ/

Comments

0

To get the html-code of a DOM-Node you can use the property outerHTML

var html = document.getElementById('someId').outerHTML;

this sadly doesnt work in firefox. But you can use XMLSerializer to achieve the same result. Like this

var elm = document.getElementById('someId');
var xmls = new XMLSerializer();
var html = xmls.serializeToString(elm);

in your jQuery-code this might look something like this:

$(".aclass").live("mousedown",function() {
  alert( this.outerHTML || new XMLSerializer().serializeToString(this)  );
});

1 Comment

Seems like XMLSerializer returns proper XML, this might give unexpected results, see: stackoverflow.com/questions/1700870/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.