javascript - White spaces getting removed on generating csv files from HTML dropdown list -
i want csv file downloaded white spaces preserved in between words.
html
<select id="ddl"> <option value="1">one apple</option> <option value="2">two oranges</option> <option value="3">three grapes</option> </select>
javascript
var ddlarray= new array(); var ddl = document.getelementbyid('ddl'); (i = 0; < ddl.options.length; i++) { ddlarray[i] = ddl .options[i].text; } var csvrows = []; (i = 0; < ddlarray.length; i++) { csvrows.push(ddlarray[i]+","); } var csvstring = csvrows.join("%0a"); var = document.createelement('a'); a.href = 'data:attachment/csv,' + csvstring; a.target = '_blank'; a.download = 'sampledownload.csv'; document.body.appendchild(a); a.click();
when executing above code, sampledownload.csv getting downloaded, option values getting trimmed white spaces lost. example "oneapple" instead of "one apple". possible preserve white spaces in csv file?
if execute alert value contain white space.
please help.
the snippet can found in jsfiddle: click here: http://jsfiddle.net/p25x8gfw/8/
this download samledownload.csv on machine. works in chrome , firefox.
try this...
var ddlarray= new array(); var ddl = document.getelementbyid('ddl'); (i = 0; < ddl.options.length; i++) { ddlarray[i] = ddl .options[i].text.replace(' ', '%20'); } var csvrows = []; (i = 0; < ddlarray.length; i++) { csvrows.push(ddlarray[i].replace(' ', '%20')+",");} var csvstring = csvrows.join("%0a"); var = document.createelement('a'); a.href = 'data:attachment/csv,' + csvstring; a.target = '_blank'; a.download = 'sampledownload.csv'; document.body.appendchild(a); a.click();
here csv file consider %20 whitespace
Comments
Post a Comment