I would need to get only the text from certain array. So I need to parse it. I have the following code:
for(var x=0;x<contentArray.length;x++){
markup += '<td>'+contentArray[x+1] +'</td>';
x++;
}
Which gives the following output:
<td><c><ul><li>The content text</li></ul></c></td>
And it loks on browser like this:
<ul><li>The content text</li></ul>
Now I would like to get only the text (The content text& in this case). How am I able to do tit?
Andrew
14.5k4 gold badges47 silver badges66 bronze badges
1 Answer 1
You can use this function to unescape HTML (stolen from the Prototype source code):
function unescapeHTML(html) {
return html
.replace(/</g,'<')
.replace(/>/g,'>')
.replace(/&/g,'&');
}
You can then use jQuery's parsing capability to get the text from the tags:
for(var x=0;x<contentArray.length;x++){
var $el = $(unescapeHTML(contentArray[x+1])).find('li'); //use the unescaped HTML to construct a jQuery object and find the li tag within it
markup += '<td>' + $el.text() + '</td>'; // get the text from the jQuery object and insert it into the fragment
x++;
}
answered Nov 10, 2010 at 13:51
lonesomeday
239k54 gold badges330 silver badges329 bronze badges
Sign up to request clarification or add additional context in comments.
5 Comments
sasss
This doesnt work. It kills the whole code/app with this. Why is there $ before el? Gives this error: uncaught exception: Syntax error, unrecognized expression: (some text which inside of that ul)
sasss
Gives this error in firebug: uncaught exception: Syntax error, unrecognized expression: (The content text)
lonesomeday
@sasss If you could provide the contents of
contentArray, I might be able to help.sasss
Something like this: <c>Texttext</c>,<c>Texttext</c>,<c>Texttext</c>,<c><ul><li>Texttext</li></ul></c>,<c>Texttext</c>,<c>Texttext</c>,<c>Texttext</c>,<c>Texttext</c>,<c>Pulling action</c>,<c>Texttext</c>,<c>Texttext</c>,<c>Texttext.</c>,<c>Texttext</c>,<c>Texttext</c> The Texttext is obviously something else in real. But text neverthless.
lonesomeday
@sasss I've edited my answer. Try now. I'm still not clear exactly what you're trying to do, though. If you could edit your question to give the input and the expected output, that would be great.
default
x++at the end of theforloop? It's already in its condition. Why is there a<c>tag? Can you give us the content of the array or where it comes from?