Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get self HTML code with jQuery [duplicate]

Tags:

jquery

<div>
  <a href="#" class="selected">link1</a>  
  <a href="#">link1</a>
</div>

and using following

$('.selected').html() I get

link1

as a return value.

How can I get full html code of the selected DOM element, in this example to get

<a href="#" class="selected">link1</a>

instead?

thanks

like image 520
Kupe3 Avatar asked Dec 27 '11 14:12

Kupe3


People also ask

How copy HTML using jQuery?

To clone an element using jQuery, use the jQuery. clone() method. The clone() method clones matched DOM Elements and select the clones. This is useful for moving copies of the elements to another location in the DOM.

What is clone() in jQuery?

jQuery clone() Method The clone() method makes a copy of selected elements, including child nodes, text and attributes.

How do I get all HTML elements in jQuery?

To get HTML content of an element using jQuery, use the html() method. The html() method gets the html contents of the first matched element.

How do you replace an element with another in jQuery?

We can replace HTML elements using the jQuery . replaceWith() method. With the jQuery replaceWith() method, we can replace each element in the set of matched elements with the provided new content and return the set of elements that were removed.


1 Answers

jQuery object:

$('.selected')

become to DOM object:

$('.selected')[0]

UPDATE:

var str = $("<div />").append($('.selected').clone()).html();
console.log(str);
like image 84
akai Avatar answered Oct 10 '22 21:10

akai