I’ve got content coming back from a webservice I cannot modify that returns data such as:
<div id="entrance" style="">
Habilitação / Diploma de Ensino Medio Diploma de Ensino Pre Universitario
<br>
<br>
Diploma Record of study
</div>
How can I wrap the two sentences in separate < p > tags using jQuery? I know I could use .wrap but that would wrap the entire contents. Do I need a RegEx?
Thanks,
Thomas
UPDATE:
My requirements changed to needing a list instead of paragraphs so using the answer below, I modified it slightly to wrap in < li > and then the whole thing in a < ul >
jQuery('#entrance').show();
jQuery('#entrance').html(data).contents().each(function () {
if ( this.nodeType === 3 ) jQuery( this ).wrap( '<li />' );
else jQuery( this ).remove();
});
jQuery('#entrance').wrapInner('<ul />');
How about:
Live demo: http://jsfiddle.net/ZMD6m/5/
My code transforms all non-empty text-nodes to paragraphs containing that text. All other types of nodes are removed.