Search code examples
javascriptjquerycursortextareaexternal

externalize content of a typeInTextarea in a text file


I'm currently trying to externalize the content of a typeInTextarea in a text file. The idea is when I click a button, all the content of a text file goes inside a textarea where the cursor is (I don't want the new content to erase the old content)... I use that code :

function typeInTextarea(el, newText) {
  var start = el.prop("selectionStart")
  var end = el.prop("selectionEnd")
  var text = el.val()
  var before = text.substring(0, start)
  var after  = text.substring(end, text.length)
  el.val(before + newText + after)
  el[0].selectionStart = el[0].selectionEnd = start + newText.length
  el.focus()
  return false
}
$("button").on("click", function() {
  typeInTextarea($("textarea"), "  SOME TEXT  ")
  return false
})

and I'm trying to achieve something like that :

$("button").on("click", function() {
  typeInTextarea($("textarea"), "  SOMETEXT.txt  ")
  return false
})

Solution

  • //SOLVED// with php

    my php

    <?php
    $file3 = file_get_contents('media/test3.txt');
    $file4 = file_get_contents('media/test4.txt');
    ...
    ...
    ?>
    

    my html

    <a href="" data-text="<?php echo $file3 ?>">Test 1</a> test3<br/>
    <a href="" data-text="<?php echo $file4 ?>">Test 2</a> test4<br/>
    <textarea rows="5" cols="40" name="replycontent" id="replycontent"></textarea>
    

    my js

    <script type="text/javascript">
        $("a[data-text]").click(function(){
      var value = $("#replycontent").val();
       $("#replycontent").val(value+$(this).attr("data-text"));
       return false;
    })
    </script>
    

    php is always helping