I give up! I looked at many different answers. I’ve tried many different ways and nothing works. I want to change the </blackquote>
tag to <br />
or a new line in the textarea. Alternatively, change to some other character, because later I can replace another character in PHP to <br/>
. How to do it?
Working example for easy understand here: https://jsfiddle.net/jsf88/rb3xp7am/35/
<textarea id="comment" name="quote" placeholder="quote" style="width:80%;height:200px;"></textarea>
<section class="replyBox" style="width: 100%;"><br/>
<a href="#napisz" class="quoteMsg"> [ click for quote ] </a>
<div class="replyMsg">
<blockquote>this is a quote for comment😎 </blockquote><br />
"X" -- HERE I want BR_TAG or new line in textarea after click 'quote' 😐
</div>
</section>
$(document).on('ready', function() {
$('.quoteMsg').click(function() {
var txt = $(this).closest('.replyBox').find('.replyMsg').text();
//txt = txt.replace('</blockquote>', '<br/>');
//txt = txt.replace(/</(blockquote)>/g, "<br/>");
//txt = txt.replace(/blockquote*/g, '<br/>');
//txt = txt.replace(/(.*?)</blockquote>(.*?)/g, ' xxx ');
txt = txt.replace(/</blockquote>/gi, '<br/>')//NOT WORKING!!
txt = txt.replace(/(?:rn|r|n)/g, ' ');//working great
console.log(txt);
$("textarea[name='quote']").val($.trim('[quote]' + txt + '[/quote]'));
});
});
To make it funnier, another example with changing the blackquote tag to br works without a problem. Why? can someone explain it?
//OTHER EXAMPLES WHERE CHANGE </BLACKQUOTE> to <br/> WORKING GOOD... WTF?!
string = ` <blockquote>this is a quote for comment😎 </blockquote><br />"X" -- HERE I want BR_TAG or new line in textarea after click 'quote' 😐`;
string = string
.replace(/</blockquote>/gi, ' <br /> ');//but here working! ;/
console.log(string);
3
Answers
Well, thanks for answers. The problem was a missing .html tag. This script work for me almost perfect for quoting few times:
The problem here is I dont know how to change dubble [hr][hr] for nothing, because this
txt = txt.replace(/[hr][hr]/g, "");
not working, so would be cool for more explain about. One more time big thanks for answers! this function .replace is not as intuitive as in PHP.EDIT: ahh.. I think is not possible to delete this dubel, because I extra insert it two times. Nvm. I will find and del this dubel in PHP.
The first problem in your code was how you were adding the event listener to the
ready
event. Being it something invented by jQuery, and not a native event, the correct way to do it should be as of now (v.3.3.1 the version I used in this demo)$(document).ready(()=>{/*code here*/})
.As a further reference:
https://api.jquery.com/ready/
But… it’s not perfectly clear how did you wish to transform your text before setting the value of the textarea. So I just better factored your logic so that you have some clear steps:
grabbing the blockquote element text content and trimming it (being the origin)
applying the transform newline to whitespace (with the regex that I left untouched)
build the final string as a template literal that will include the quote content, the meta tags wrapping it, AND anything else you wish to add like for example a new line (
n
) that in this example is exacerbated by a text following it.There’s a hint in your words that put me in the position to say something superflous but still deserving an attempt: the value of a inner text is just plain text and doesn’t render html content. So the
<br>
itself would remain as you read it and wouldn’t have any rendering effect on the textarea content. That’s why I focused my demonstration on putting a newline with the escaping sequence. It works both on double quoted strings and template literals:"n"
`n`
Further notes
It seems the original approach of processing the blockquote html was preferred. It’s worth saying that it was appearently a terrible strategy for several reasons:
rendered on the page.
wrapping blockquote tags instead of fetching directly the innerHTML.
<br>
soat this point I ask myself if the content in the textarea was
supposed to be encoded html or not.. and the added br would then
belong to something meta?
string processing
But… maybe there’s something I didn’t get and I’m doing weak assumptions.
('.replyMsg').text()
but in that case you will have the text but with no html tag like<blockquote>
so first you will have to recover the html to have the blockquote tagvar txt = $(this).closest('.replyBox').find('.replyMsg').html();
txt = txt.replace(/<blockquote>/gi, '');