Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: wypełnanie textarea
Forum PHP.pl > Forum > Po stronie przeglądarki > JavaScript
dyktek
Witam, zastanawiam się jak ugryźć problem wypełniania pola textarea w taki sposób jak jest to zrealizowane tutaj na forum. Są znaczniki B I U w momencie gdy klikne na butonnik wklejany jest odpowiedni kod w miejsce kursora.
revyag
Było o tym na forum, wystarczy poszukać. Do googli też warto zajrzeć:
cursor + textarea
dyktek
ok poradziłem sobie, dzięki za info winksmiley.jpg

  1. <script type="text/javascript">
  2. var cnt = new Array();
  3.  
  4. function kontr(bCode){      
  5.      var regKr = /(\*)/;      
  6.      var eLem = document.getElementById(bCode).value;      
  7.      regKr.test(eLem)?document.getElementById(bCode).value = '[ ' + bCode + ' ]' : document.getElementById(bCode).value = '[ ' + bCode + ' ]*';      
  8. }
  9.  
  10. function naw(N){
  11.      var regKr = /(\*)/;
  12.      var vN = document.getElementById(N).value;
  13.      return regKr.test(vN);
  14. }
  15.  
  16. function Paste(co){
  17.      kontr(co);
  18.      var ff = document.getElementById('tresc');
  19.            if (!cnt[co]) cnt[co] = 0;
  20.  
  21.            if(document.selection){
  22.                  document.formularz.tresc.focus();
  23.                  document.selection.createRange().text = naw(co)?'['+co+']':'[/'+co+']';
  24.            }
  25.            else if (ff.selectionStart || ff.selectionStart == 0) {
  26.                  ff.focus();
  27.                  var bC = naw(co)?'['+co+']':'[/'+co+']';
  28.                  ff.value = ff.value.substring(0,ff.selectionStart)+bC+ff.value.substring(ff.selectionEnd,ff.value.length);
  29.            }
  30.      cnt[co]++;
  31. }
  32.  
  33.  
  34. <input type="button" id="b" name="b" value="[ b ]" onClick="Paste('b');">
  35. <input type="button" id="i" name="i" value="[ i ]" onClick="Paste('i');">
  36. <input type="button" id="u" name="u" value="[ u ]" onClick="Paste('u');">
  37. <br /><br />
  38. <form id="formularz" name="formularz">
  39. <textarea id="tresc" name="tresc" style="width:400px; height:200px"></textarea>
  40. </form>
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.