Home > Net >  txt.replace </blockquote> in textarea js
txt.replace </blockquote> in textarea js

Time:12-21

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  style="width: 100%;"><br/>
  <a href="#napisz" > [ click for quote ] </a>
  <div >

    <blockquote>this is a quote for comment&#128526; </blockquote><br />
    "X" -- HERE I want BR_TAG or new line in textarea after click 'quote' &#128528;

  </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(/(?:\r\n|\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&#128526; </blockquote><br />"X" -- HERE I want BR_TAG or new line in textarea after click 'quote' &#128528;`;

string = string
.replace(/<\/blockquote>/gi, ' <br /> ');//but here working! ;/
console.log(string);

CodePudding user response:

  1. you recover text with text function ('.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 tag var txt = $(this).closest('.replyBox').find('.replyMsg').html();
  2. the br tag is not interpreted in textarea so you have to change it by a new line character
  3. don't forget to remove opened bloquote tag to get the expected result

txt = txt.replace(/<blockquote>/gi, '');

$('.quoteMsg').click(function() {
  var txt = $(this).closest('.replyBox').find('.replyMsg').html();
  txt = txt.replace(/(?:\r\n|\r|\n)/g, ' ');
  txt = txt.replace(/<\/blockquote>/gi, '\n');
  txt = txt.replace(/<blockquote>/gi, '');

  console.log(txt);
  $("textarea[name='quote']").val($.trim('[quote]'   txt   '[/quote]'));
});
blockquote {
  background-color: silver;
}

.replyMsg {
  border: 2px solid green;
}

.quoteMsg {
  background-color: green;
  color: #fff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea id="comment" name="quote" placeholder="quote" style="width:80%;height:200px;"></textarea>
<section  style="width: 100%;"><br/>
  <a href="#napisz" > [ click for quote ] </a>
  <div >

    <blockquote>this is a quote for comment&#128526; </blockquote>

    "X" -- HERE I want BR_TAG or new line in textare a after c lick 'quote' &#128528;

  </div>
</section>

CodePudding user response:

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/

There is also $(document).on( "ready", handler ), deprecated as of jQuery 1.8 and removed in jQuery 3.0. Note that if the DOM becomes ready before this event is attached, the handler will not be executed.

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:

  • It grabs the blockquote content as html despite that's not how it's rendered on the page.
  • It takes the effort to consider the whole outerHTML removing the wrapping blockquote tags instead of fetching directly the innerHTML.
  • It adds the newline as newline instead of embedding it as <br> so at 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?
  • It's harder to deal with in case you want to further customize the string processing

But... maybe there's something I didn't get and I'm doing weak assumptions.

//since you are using the ready event with jquery, that's the correct syntax
$(document).ready(function() {
  $('.quoteMsg').click(function() {

    //grabs the text content of the blockquote element (trimming it)
    var quoteTextContent = $(this).closest('.replyBox').find('.replyMsg').text().trim();
    
    //performs the transform already in place in your code.. replacing newlines with white spaces
    quoteTextContent = quoteTextContent.replace(/(?:\r\n|\r|\n)/g, ' '); //working great    
    
    //builds the string to set the textarea value with, using a template literal
    //here you can add anything you want.. like a new line but that's just an example
    const encoded = `[quote]${quoteTextContent}[/quote]\nand something following to show the new line happening`;

    console.log(encoded);
    $("textarea[name='quote']").val( encoded );
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea id="comment" name="quote" placeholder="quote" style="width:80%;height:200px;"></textarea>
<section  style="width: 100%;"><br/>
  <a href="#napisz" > [ click for quote ] </a>
  <div >
    <blockquote>this is a quote for comment&#128526;
    <br>
    Having new lines also ... since you perform a regex transform newline=>whitespace
    </blockquote><br />
  </div>
</section>

CodePudding user response:

Well, thanks for answers. The problem was a missing .html tag. This script work for me almost perfect for quoting few times:

$(document).on('ready', function() {
  $('.quoteMsg').click(function() {
    var txt = $(this).closest('.replyBox').find('.replyMsg').html();    
  txt = txt.replace(/(?:\r\n|\r|\n)/g, ' ');
    txt = txt.replace(/&lt;/g, "<");
    txt = txt.replace(/&gt;/g, ">");
    txt = txt.replace(/&amp;/g, "&");
    txt = txt.replace(/&quot;/g, '"');
    txt = txt.replace(/&#039;/g, "'");
    txt = txt.replace(/<br>/g, "");
    txt = txt.replace(/<hr>/g, "[hr]");
    //txt = txt.replace(/<hr>/g, "\n");
  txt = txt.replace(/<blockquote>/gi, '');
  txt = txt.replace(/<\/blockquote>/gi, '[hr]');
    txt = txt.replace(/[hr][hr]/gi, "");//not working ([][])
    txt = txt.replace(/[hr][hr]/gi, "[hr]");//not working ([[hr]][[hr]])
    console.log(txt);
    $("textarea[name='quote']").val($.trim('[quote]'   txt   '[/quote]\n'));
  });
});

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.

  • Related