How to convert an html string into a jQuery object?

8

How to convert an HTML to a jQuery object?

var objeto = '<div id="minhaDiv"><span id="meuSpan">Span</span></div>';
    
asked by anonymous 18.09.2015 / 19:09

2 answers

11

Very simple, just instantiate jQuery by passing HTML as a parameter.

var teste = $('<div id="minhaDiv"><span id="meuSpan">Span</span></div>').find("span").text();
$("#result").html(teste);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><divid="result"></div>
    
18.09.2015 / 19:12
8

You can use innerHTML native. Example

var objeto = '<div id="minhaDiv"><span id="meuSpan">Span</span></div>';
document.getElementById("ID_DIV").innerHTML = objeto ;

Or you can use JQuery :

var objeto = '<div id="minhaDiv"><span id="meuSpan">Span</span></div>';
$("#ID_DIV").html(objeto );
    
18.09.2015 / 19:13