Skip to content Skip to sidebar Skip to footer

Remove HTML Tags From Javascript Output

I looked around stackoverflow but I couldn't find an answer that solved my problem. I hope I can explain it so you all understand what I'm trying to do... (If more info is needed,

Solution 1:

It sounds like data is a string containing markup in the form <p style="font-size:1.2rem">STUFF HERE</p>, repeated, and you want to get just the STUFF HERE part.

The reliable way to do that is to parse the HTML and extract the text from it:

// The data
var data =
    '<p style="font-size:1.2rem">First</p>' +
    '<p style="font-size:1.2rem">Second</p>' +
    '<p style="font-size:1.2rem">Third</p>';
// Parse it
var elements = $.parseHTML(data);
// Convert that to an array of the text of the entries
var entries = elements.map(function(element) {
  return $(element).text();
});
// Get a string of the array entries joined with "\n"
var value = entries.join("\n");
// Done!
$("#output").val(value);
<textarea id="output" rows="4"></textarea>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

That can be written more concisely, but I kept the parts separate for clarity.


Post a Comment for "Remove HTML Tags From Javascript Output"