Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get HTML fragment without text by jQuery

I have a HTML code with div container and another HTML element and text inside it

<div id="container"><i class="myico"></i> text</div>

I need to get only HTML element from the container without the text.

So i need to get only

<i class="myico"></i>

How can I get it using jQuery?

like image 645
Cichy Avatar asked Nov 03 '22 10:11

Cichy


1 Answers

Simply to get the element use one of the following:

var element = $("#container > i");
var element = $("#container i");
var element = $("#container .myico");
var element = $("#container").find("i.myico");

To get the element out of the markup use detach():

var element = $("#container > i").detach();

Then to get an HTML code, you may use outerHTML property:

var html = element.get(0).outerHTML;

DEMO: http://jsfiddle.net/tLvdZ/

like image 94
VisioN Avatar answered Nov 08 '22 15:11

VisioN