简体   繁体   中英

How to get textarea line breaks into generated file with .val()

I would like to download content of textarea into file but also remain linebreaks.

I was doing it with .html() function, but then I needed to be able to edit the textarea, so I used .val() to insert values to textarea. It worked fine, but after the change .html() doesn't read the content, only val() does. But the file that it downloads doesn't contain linebreaks that are necessary for the purpose.

Content of textarea is being set through for-loop with adding "\\n" at each line end.

I am using filerSaver.js for downloading files

var content = $( '#cdr_container' ).val();

var blob = new Blob([content], {
    type: "text/plain;charset=utf-8"
});

saveAs(blob, filename);

I need to get those linebreaks into file as well. Is there any work around? Thank you for help.

Solved by a simply adding this in front of text declaration

$.valHooks.textarea = {
    get: function( elem ) {
        return elem.value.replace( /\r?\n/g, "\r\n" );
    }
};

Here is the issue described

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM