CodeIgniter Forums
[Solved] Textarea indent 4 spaces jQuery & Codeigniter - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: [Solved] Textarea indent 4 spaces jQuery & Codeigniter (/thread-66940.html)



[Solved] Textarea indent 4 spaces jQuery & Codeigniter - wolfgang1983 - 12-20-2016

When I click on my button "c" it should indent the selected text by four spaces. How ever it works fine for one line but if I select multiple lines it only indents the very first line I need it to indent all selected text.

Question how to indent multiple selected lines if.

Live example here

PHP Code:
$('.c').click(function() {
 
   wrapText("message""   """);
}); 


PHP Code:
function wrapText(elementIDopenTagcloseTag) {
 
   var textArea = $('#' elementID);
 
   var len textArea.val().length;
 
   var start textArea[0].selectionStart;
 
   var end textArea[0].selectionEnd;
 
   var selectedText textArea.val().substring(startend);
 
   var replacement openTag selectedText closeTag;
 
   textArea.val(textArea.val().substring(0start) + replacement textArea.val().substring(endlen));
}

$(
'.b').click(function() {
 
   wrapText("message""**""**");
});

$(
'.i').click(function() {
 
   wrapText("message""*""*");
});

$(
'.c').click(function() {
 
   wrapText("message""   """);
}); 



RE: Textarea indent 4 spaces jQuery & Codeigniter - salain - 12-21-2016

Hi,

I have not tested it, but is should work.
What you need to do is to find the newline and add your startTag to each line.
Because of different character for new line on Windows, Linux and Mac you need to test for all 3.
You will notice I use the \r\n (Windows linebreak) to insure it works on most systems


PHP Code:
function wrapText(elementIDopenTagcloseTag) {
 
   var textArea = $('#' elementID);
 
   var len textArea.val().length;
 
   var start textArea[0].selectionStart;
 
   var end textArea[0].selectionEnd;
 
   var selectedText textArea.val().substring(startend);
 
   var replacement openTag selectedText closeTag;

 
   replacement.replace(/\r\n|\r|\n/g,"\r\n"+openTag);

 
   textArea.val(textArea.val().substring(0start) + replacement textArea.val().substring(endlen));


Source 1 for the solution
Source 2


RE: Textarea indent 4 spaces jQuery & Codeigniter - wolfgang1983 - 12-21-2016

(12-21-2016, 12:20 AM)salain Wrote: Hi,

I have not tested it, but is should work.
What you need to do is to find the newline and add your startTag to each line.
Because of different character for new line on Windows, Linux and Mac you need to test for all 3.
You will notice I use the \r\n (Windows linebreak) to insure it works on most systems


PHP Code:
function wrapText(elementIDopenTagcloseTag) {
 
   var textArea = $('#' elementID);
 
   var len textArea.val().length;
 
   var start textArea[0].selectionStart;
 
   var end textArea[0].selectionEnd;
 
   var selectedText textArea.val().substring(startend);
 
   var replacement openTag selectedText closeTag;

 
   replacement.replace(/\r\n|\r|\n/g,"\r\n"+openTag);

 
   textArea.val(textArea.val().substring(0start) + replacement textArea.val().substring(endlen));


Source 1 for the solution
Source 2

It's only when I click on my button for it I though And I am not after line break


RE: Textarea indent 4 spaces jQuery & Codeigniter - salain - 12-21-2016

Oops,
I misunderstood, however this would indent the selected text line by line.
The only other way I can think of it would be to use an editor for the textarea.

Sorry about that, I hope you find a solution.


RE: Textarea indent 4 spaces jQuery & Codeigniter - wolfgang1983 - 12-21-2016

Solution is here now Asked on another forum