var csvData = 'data:application/csv;charset=utf-8,' + $('#res').append(' ').text() + $('#res1').append(' ').text(); 

I can not understand how to put a space that would divide the numbers
$('#res').append(' ').text() - contains an arbitrary number for example 1000

 $('#res1').append(' ').text() - содержит тоже произвольное число например 0001 

I write these two numbers to a file (*. txt), at the moment an entry in the file is 10000001. How can I divide these numbers with a space or transfer to another line? I want to achieve this result:

1000 0001


1000
0001

  • append() is used to add an element to the DOM, and not to add spaces to the text. - teran
  • $ ('# res'). text () + '' + $ ('# res1'). text (); - teran
  • make a minimal reproducible example , everything looks working - Grundy
  • @teran tried, doesn't work like that. Spacebar does not set - Eliot
  • one
    @teran, and where is the space? $('#res').text() + ' ' + $('#res1').text(); - user207618

1 answer 1

It is necessary to encode the data encodeURIComponent function.
And you need the download attribute:

 let f = document.querySelector('#f'), s = document.querySelector('#s'), d = document.querySelector('#d'), l = document.createElement('A'); l.setAttribute('download', 'fileName.scv'); document.body.appendChild(l); d.addEventListener('click', e => { l.href = `data:application/csv;charset=utf-8,${encodeURIComponent(f.value + ' ' + s.value)}`; l.click(); }); 
 F num: <input type='text' id='f' value='123' /><br /> S num: <input type='text' id='s' value='456' /><br /><br /> <input type='button' id='d' value='Download' />