Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For some reason, I was never alerted when this post was updated...and last night, I had this BRILLIANT idea on how to determine where the line breaks were... I would rebuild the string, and check the width each time, and it WORKED so I came here to share it...and found I was 1 week behind</p> <p>Anyway 2 important things</p> <ol> <li><p>The code you provided uses the same brilliant idea I had (well done you) BUT when I test it, it breaks the first line correctly then adds a line break after every character (tested on the link jsfiddle.net)</p></li> <li><p>I've added my code which uses jquery and uses the width of a span to determine when to break At first I tried using the width of the div, but div.width() returns the default width, not the width of the content.</p></li> </ol> <p><strong>I AM AWARE THIS MAY NOT WORK ON ALL BROWSERS</strong> so, I ask kindly that if anyone knows of a way of making this foolproof, or close to it, please share.</p> <p>First, the styles are necessary to synchornize fonts (all attributes) between the textarea and div, set the size, and (for IE) remove any scrollbars that automatically appear.</p> <pre><code> .inputArea { width:200px; height:100px; font-family:Arial; font-size:12px; overflow: auto; border: 1px solid #cccccc; padding:0; margin:0; } .divArea { font-family:Arial; font-size:12px; } </code></pre> <p>Next, I include jquery and my custom functions:</p> <pre><code> &lt;script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js "&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).ready(function() { $("#breakUp").click(function () { showLineBreaks(); addLineBreaks(); }); function showLineBreaks() { content = $("#textEntered").val(); //replace line breaks in content with "|" to allow for replacement below content = content.replace("\r\n", "<br>"); content = content.replace("\r", "<br>"); content = content.replace("\n", "<br>"); $("#unedited").html(content); } function addLineBreaks() { content = $("#textEntered").val(); //replace line breaks in content with "|" to allow for replacement below content = content.replace("\r\n", "|"); content = content.replace("\r", "|"); content = content.replace("\n", "|"); tempContent = ""; $("#edited").html(""); for (var i = 0; i "); } else { tempContent = $("#edited").html(); $("#edited").html(tempContent + content.charAt(i)); if ($("#edited").width() > 200) { $("#edited").html(tempContent + "<br>" + content.charAt(i)); } } } } }); &lt;script&gt; </code></pre> <p>And finally, my html test page</p> <pre><code> Enter text into the textarea below (Set to 200 px width, 100 px height)&lt;br&gt; &lt;textarea id="textEntered" class="inputArea"&gt;&lt;/textarea&gt; &lt;br&gt;&lt;br&gt; The div below will display that text WITHOUT wrapping, BUT replacing all existing line breaks with &lt;br&gt;&lt;br&gt; &lt;div id="unedited"&gt;&lt;/div&gt; &lt;br&gt; The following div will display that text with line breaks ADDED to fit the wrapping&lt;br&gt; &lt;div class="divArea"&gt;&lt;span id="edited"&gt;&lt;/span&gt;&lt;/div&gt; &lt;br&gt; &lt;button id="breakUp"&gt;Click Here to Convert&lt;/button&gt;</code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload