Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 67411

jquery textarea set value but \n doesn't work

$
0
0

I'm using codeigniter and the controller send a value to view (I want to show it in textarea).
the value contains breakline \n as the data was inputted with textarea tag.
and I'm using jquery to read its data. I don't read it on html section (which will work perfectly) because it's easy for me to recognize and maintain every function like delete section/element, or add section/element.
I've been looking for any similar problem, but none is working.
so here is my code:

<script>
function init()
{
    var render = '<textarea required id="some_id"><?= $test ?></textarea>
    $('#some_div').html(render);
}
</script>

so there's an error and the result looks like this on my console: enter image description here

so there are breakline and I'm guessing the problem is with the breakline because when declare a string value, it needs '+ at the end of it.
so I try to add it and here is my code.

<script>
function init()
{
    <?php

        $text = $some_data_from_controller;
        $breaks = array("\r\n");  
        $test = str_ireplace($breaks, "'+\n'", $text);  

    ?>

    var render = '<textarea required id="some_id"><?= $test ?></textarea>';
    $('#some_div').html(render);
}
</script>

and this is on my console:
enter image description here

so there's no error appear. but the breakline doesn't work. Here's the result on my web:
enter image description here


Viewing all articles
Browse latest Browse all 67411

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>