I want to split articles (= HTML content) that I receive from a webservice in different DIVs, based on a HR tag.
I explain with an example, this is what I receive from the service:
<p>This is an article bla bla</p> <hr/> <p>this is the next page of the article blabla ...</p>
I want to make, based on the HR-tag:
<div><p>This is an article bla bla</p></div>
<div><p>this is the next page of the article blabla ...</p></div>
I tried different thinks but is doesn't work. How can I do that with Javascript or JQuery (or with another method ;-))?
I want to split articles (= HTML content) that I receive from a webservice in different DIVs, based on a HR tag.
I explain with an example, this is what I receive from the service:
<p>This is an article bla bla</p> <hr/> <p>this is the next page of the article blabla ...</p>
I want to make, based on the HR-tag:
<div><p>This is an article bla bla</p></div>
<div><p>this is the next page of the article blabla ...</p></div>
I tried different thinks but is doesn't work. How can I do that with Javascript or JQuery (or with another method ;-))?
Share Improve this question edited Nov 12, 2011 at 20:04 Adam Rackis 83.4k57 gold badges278 silver badges400 bronze badges asked Nov 12, 2011 at 8:48 benskebenske 4,0504 gold badges26 silver badges22 bronze badges2 Answers
Reset to default 6Use split to create and array and loop through to create the divs.
var html = "<p>This is an article bla bla</p> <hr/> <p>this is the next page of the article blabla ...</p>";
$(html.split('<hr/>')).each(function(){
$('#test').append('<div>'+this+'</div>')
})
Demo
Try -
var html = '<p>This is an article bla bla</p> <hr/> <p>this is the next page of the article blabla ...</p>'
var htmlarr = html.split('<hr/>');
html = html.replace(/<hr\/>/g,'');
$('body').append(html);
This will just remove all the <hr/>
s then append the remaining contnet to the page.Which I think will achieve what you want without performing a split
. You could do a split like this -
var htmlarr = html.split('<hr/>');
Which would leave you with an array containing the two bits of HTML you list in your question. You could add this to your page using -
$.each(htmlarr,function (index,value) {
$('body').append(value);
});
Demo - http://jsfiddle/cCR34/2