3

I use this code to create a "fake text area" (content editable div) and some jQuery stuff to fake a placeholder and pass the text to a real textarea:

/*If used as update clear placeholders*/
$( window ).on("load", function(){
    $( document ).find('.feditInput').each( function(){
        if ($(this).html().length > 1) {
            $(this).parent().find(".jqtholder").css("opacity","0");
        }
    }
)});
/*Now the main thing*/
$('.feditInput').on('change keydown keyup',function(){
    if (event.which != '9') {
        $('#'+$(this).attr('foreal')).text($(this).text()) ;
        if ($(this).html() == '' && event.which == '8') {
            $(this).parent().find(".jqtholder").css("opacity","1");
        }
       else {
           $(this).parent().find(".jqtholder").css("opacity","0");
       }
   }
});
.faketextarea
{
    position:relative;
    border: 1px solid #aaa;
    width: 20%;
    padding: 3px 2px;
}
.jqtholder
{
    position:absolute;
    color:gray;
    top:0;
    left:0;
    padding: inherit;
    z-index:-1;
}

.feditInput
{
    width:100%;
    outline:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="faketextarea">
    <div class="jqtholder">Subject</div>
    <div class="feditInput" foreal="1" contenteditable></div>
</div><br/>
<div class="faketextarea">
    <div class="jqtholder">Massege</div>
    <div class="feditInput" foreal="2" contenteditable></div>
</div>
<textarea id="1"></textarea>
<textarea id="2"></textarea>

My Problem is: When you use break lines in the fake textarea they dont pass into the real textarea as break lines.

for example if you type: "hi I'm john and
I love coding..."
youll get: "hi i'm john andi love coding..."

I tried playing with different combinations of .text / .html / .val with no success.

If I change this:

$('#'+$(this).attr('foreal')).val($(this).html()) ;

To this:

$('#'+$(this).attr('foreal')).val($(this).html().replace("<div>","").replace("</div>","\n").replace("<br>","\n")) ;

It will work great for the break lines but then add the &nbsp; for spaces...

This answer doesn't work in this situation for some reason. maybe someone can find out why...

Nori
  • 121
  • 8
  • They *are* passed along, it's just that a newline doesn't really mean anything in HTML, so it's not shown. You'd have to replace those newlines with actual `
    ` tags.
    – adeneo Jun 11 '17 at 15:03
  • So what is happening when i press enter in a real textarea and it shows me a newline and how can i convert the newline of the fake textarea to that...? – Nori Jun 11 '17 at 15:04
  • https://stackoverflow.com/questions/784539/how-do-i-replace-all-line-breaks-in-a-string-with-br-tags – adeneo Jun 11 '17 at 15:05
  • thanks for the link, but for some reason can't seam to make it work with those answers... – Nori Jun 11 '17 at 15:10
  • Use `html()` instead of `text()` – adeneo Jun 11 '17 at 15:12
  • i did but can't get it to work, did you try it with the code? – Nori Jun 11 '17 at 15:26
  • Possible duplicate of [How do I replace all line breaks in a string with
    tags?](https://stackoverflow.com/questions/784539/how-do-i-replace-all-line-breaks-in-a-string-with-br-tags)
    – Alexander Tsvetkov Jun 11 '17 at 19:46
  • I guess you didn't read the pervious comments? – Nori Jun 12 '17 at 03:09

0 Answers0