Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript split() messes up my html tags
    primarykey
    data
    text
    <p>I have a 2500-words string that I need to be put in different <code>DIV</code>. I have used the <code>text = text.split(' ')</code> function to retrieve each word and then have my <code>DIVs</code> filled until <code>DIV.scrollHeight</code> becomes higher than <code>DIV.offsetHeight</code>, then it goes to the next <code>DIV</code>.</p> <p>The problem I encounter is that my HTML tags in my 2500-word string are completely messed up: it closes my <code>&lt;b&gt;</code> tags where it shouldn't, doesn't display the <code>&lt;/b&gt;</code> where it should, doesn't display the <code>&lt;font</code> part of this tag, doesn't display the <code>&lt;/font&gt;</code>, etc.</p> <p>Any idea why?</p> <p>Many thanks!</p> <p>Edit: As requested, here is the entire jQuery code. Pardon me in advance for any mistake in it :)</p> <pre><code>function addText(texte) { // var texte = texte.replace('&lt;', '&amp;lt;'); // var texte = texte.replace('&gt;', '&amp;gt;'); var container = $('DIV[id^=apDiv]').find('DIV#bloc_prim'); console.log('NOMBRE DE CONTAINER : '+container.length); var length = texte.length; console.log('LONGUEUR DU TEXTE '+length+' caractères'); // container.html(texte); var hauteurDiv = container.prop('offsetHeight'); var hauteurContent = container.prop('scrollHeight'); length = Math.floor((length * hauteurDiv)/hauteurContent); console.log(hauteurContent); // container.html(texte.substring(0, length)); var hauteurRestante = hauteurContent - hauteurDiv; console.log(hauteurRestante); var TABLEAU = texte.split(' '); // var TABLEAU = texte.match(/&lt;\s*(\w+\b)(?:(?!&lt;\s*\/\s*\1\b)[\s\S])*&lt;\s*\/\s*\1\s*&gt;|\S+/g); console.log('LONGUEUR TABLEAU : '+TABLEAU.length+' occurences'); // console.log(TABLEAU[4]); i = 0; hauteurTotale = container.prop('scrollHeight'); console.log(container.prop('offsetHeight')); console.log(hauteurTotale); while(i &lt; TABLEAU.length) { while(hauteurTotale &lt;= container.prop('offsetHeight')) { container.append(TABLEAU[i]+' '); i++; hauteurTotale = container.prop('scrollHeight'); console.log(i+':'+hauteurTotale+' --- '+container.prop('offsetHeight')); } if(i &lt; TABLEAU.length) { var clone = container.parent('DIV[id^=apDiv]').clone(true); // On copie la DIV $('BODY').append(clone); // On colle la partie copiée clone.find('DIV#bloc_prim').empty(); } var hauteurTotale = clone.find('DIV#bloc_prim').prop('scrollHeight'); console.log(hauteurTotale); while(hauteurTotale &lt;= clone.find('DIV#bloc_prim').prop('offsetHeight')) { if(i &lt; TABLEAU.length) { clone.find('DIV#bloc_prim').append(TABLEAU[i]+' '); i++; hauteurTotale = clone.find('DIV#bloc_prim').prop('scrollHeight'); console.log(i+':'+hauteurTotale+' --- '+container.prop('offsetHeight')); } else { break; } } } console.log(i+'-&gt;'+TABLEAU.length); </code></pre>
    singulars
    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.
 

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