0

I'm trying to create a markdown editor. Now in this function I'm working on B (bold) button which is toggle. It should be noted that I use this library to get/set highlighted text from textarea.

Here is my function: (It works as well, all fine)

function toggleText(before, after) {
    var $textarea =  $('#qandatextarea');
    var textarea  =  $textarea[0];
    var sel       =  $textarea.getSelection();
    var val       =  textarea.value;
    var posStart  =  sel.start; 
    var posEnd    =  posStart + sel.length;
    var posBefore =  posStart - before.length;

    if (val.substr(posBefore, before.length) == before && val.substr(sel.end, after.length) == after) {
      textarea.value = val.slice(0, posBefore) + sel.text + val.slice(sel.end + after.length);
      $textarea.setSelection(posBefore, posBefore + sel.length);

    } else {

      $textarea.surroundSelectedText(before, after);

      while(val.substr(posStart, 1) == ' ') {
        posStart++;
        $textarea.val( textarea.value.replaceAt(posStart + 1, "*").replaceAt(posStart - 1, " ") );
      } 

      while(val.substr(posEnd - 1, 1) == ' ') {
        $textarea.val( textarea.value.replaceAt(posEnd + 1, "*").replaceAt(posEnd + after.length + 1, " ") );
        posEnd--;
      } 

      // set new highlighted-text
      $textarea.setSelection(posStart + before.length, posEnd +  before.length); 

   } // else

} // function

So for making it more optimize, I have used assignment instead of $textarea.val( in the loop, but now it doesn't work correctly, it replaces wrong characters and handles spaces badly.

.
.
.
else {

    var txtval;
    $textarea.surroundSelectedText(before, after);

    while(val.substr(posStart, 1) == ' ') {
        posStart++;
        txtval = textarea.value.replaceAt(posStart + 1, "*").replaceAt(posStart - 1, " ");
    } 

    while(val.substr(posEnd - 1, 1) == ' ') {
        txtval =  textarea.value.replaceAt(posEnd + 1, "*").replaceAt(posEnd + after.length + 1, " ");
        posEnd--;
    } 

    // set textarea value
    $textarea.val(txtval);
    $textarea.setSelection(posStart + before.length, posEnd +  before.length);    

}
.
.
.
1
  • It seems that there are two problems with your code: 1) If the character ("index, 1" or "index - 1, 1") that is not a space then the while-loop will be bypassed/skipped. 2) You never update the value of the textarea OR the string-variable ("val") meaning you are working with the same string without actually replacing anything. You should probably be using "val = ..:" in stead of "txtval = ..." inside your while-loops and in the final "set textarea value": $textarea.val(val); See formated code ( put #34887329 in the URL to jumb to the answer) in the answer section. Commented Jan 19, 2016 at 21:29

1 Answer 1

1

It seems that there are two problems with your code:

1) If the character ("index, 1" or "index - 1, 1") is not a space then the while-loop will be bypassed/skipped and no further replacements will be done.

2) You never update the value of the textarea OR the assigned variable ("val") meaning you are working with the same string without actually replacing anything. You should probably be using "val = ..." in stead of "txtval = ..." inside your while-loops and in the final "set textarea value": $textarea.val(val);

// ... code ...

    while(val.substr(posStart, 1) == ' ') {
        posStart++;
        val = val.replaceAt(posStart + 1, "*").replaceAt(posStart - 1, " ");
    } 

    while(val.substr(posEnd - 1, 1) == ' ') {
        val = val.replaceAt(posEnd + 1, "*").replaceAt(posEnd + after.length + 1, " ");
        posEnd--;
    } 

    // set textarea value
    $textarea.val(val);

// ... code... 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.