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, please let me know).
I have a piece PHP script that adds and removes content from an session array. Right now this is the code for this script:
<?php foreach ($_SESSION['products'] as $product): ?>
<p style="font-size:1.2rem"><?php echo htmlspecialchars($product); ?></p>
<?php endforeach;?>
Using Javascript I echo this to a textarea
where it returns the values.
Javascript
$('.my-subject').click(function(){
var title = $(this).attr('value');
$("textarea#thema").html(data);
})
Right now it returns the value with the HTML Tags <p>
... </p>
. How can I get it to return the value's with no HTML tags and a line break at the end? I tried using:
$("textarea#thema").text() + '\n';
But that didn't work, this still shows the <p>
-tags and no line break.
I also saw some codes like this:
function removeTags(){
var txt = document.getElementById('myString').value;
var rex = /(<([^>]+)>)/ig;
alert(txt.replace(rex , ""));
}
But I don't know if I can use that for my code and how to do this... Any help would be appreciated. Thanks.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.