How to load data from a .txt file into a text field

I am trying to populate textarea with data from a TXT file. I tried to use answers to similar questions, but I can not find the one that works. In the head of the file, I have this to import the library that is used with jQuery:

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>

Text declaration declaration:

<textarea id="fillText" cols="40" rows="5"></textarea>

This is the script tag that I have right after the textarea declaration:

<script>
var fileName = '<?php echo $fileN;?>.txt'; //gets the filename
//The follow works and alerts the browser with the contents of the text file
jQuery.get(fileName, function(data) {
   alert(data);
   //process text file line by line
   $('fillText').html(data.replace('n',''));

});
</script>

I also tried using this:

 $(".fillText").load(fileName);

But for some reason this does not work.

+4
source share
2 answers

Since you are working with textarea, you need to specify a value, not html.

Edit:

$('fillText').html(data.replace('n',''));

To:

$('#fillText').val(data.replace('n',''));

+3
source

.fillText , HTML, class="fillText"
#fillText , HTML-, id="fillText", .

, : http://www.w3schools.com/cssref/css_selectors.asp

, #, .

+2

All Articles