Vince Vince - 1 month ago 11
HTML Question

TinyMCE how to dynamically get line breaks in a textarea

I need advice on how I can create dynamic line breaks ideally with an

<li>
inside TINY. I have tried using an each loop and also a for loop, and the values just do not appear in the text area. BUT, if I just add them to the text area with a val() they go in fine, BUT as just one long string.

The text area has an id of wo_materials. I'm successfully getting my text into Tiny like this:

$('#wo_materials').val(materials);

tinymce.init({
selector:'textarea'
});


And I get a nice row of text values:

Values in TinyMCE

The materials value is an array. If I look at it in the console it looks like this:

0: BP #15 Plain Felt 36"
1: Duraflo Weatherpro 50
2: 1 1/4 Coil Nails - box


Thanks !

Answer

This works. The secret is adding the incrementing var "text" to the for loop and then wrapping the object / array in an <li>

var materials= JSON.parse(localStorage.getItem('materials'));
var text=" "; 
materials.length;
function workorders(){
            for (i = 0; i < materials.length; i++) {
               text += "<li>"+materials[i]+"</li>";
            }
            $('#wo_materials').val("<li>"+text+"</li>");
        }

        workorders();
Comments