JavaScript How to insert a space after insert node insert space after table in word,insert space after comma

I want to insert a space after insert a node, the problem is the space that I insert is inside a node, not after the a node.

I have other solution, which is inserting a span and putting a space into the span node, but i think it should have a better solution.

var $preload = $('<a target='_blank' href="url">test.txt</a>');
editor.insertNodeAtCaret($preload.get(0)); 
insertTextAtCursor('\u00A0', true);

function insertNodeAtCaret(node) {
   if (typeof window.getSelection != "undefined") {
       var sel = window.getSelection();
       if (sel.rangeCount) {
           var range = sel.getRangeAt(0);
           range.collapse(false);
           range.insertNode(node);
           range = range.cloneRange();
           range.selectNodeContents(node);
           range.collapse(false);
           sel.removeAllRanges();
           sel.addRange(range);
      }
  } else if (typeof document.selection != "undefined" && document.selection.type != "Control") {
       var html = (node.nodeType == 1) ? node.outerHTML : node.data;
       var id = "marker_" + ("" + Math.random()).slice(2);
       html += '<span id="' + id + '"></span>';
       var textRange = document.selection.createRange();
       textRange.collapse(false);
       textRange.pasteHTML(html);
       var markerSpan = document.getElementById(id);
       textRange.moveToElementText(markerSpan);
       textRange.select();
       markerSpan.parentNode.removeChild(markerSpan);
    }
};



function insertTextAtCursor(text, moveTheCursor) {
    var sel, range, html;
    if (window.getSelection) {
        sel = window.getSelection();
        if (sel.getRangeAt && sel.rangeCount) {
            range = sel.getRangeAt(0);
            range.deleteContents();
            var textNode = document.createTextNode(text);
            range.insertNode(textNode);
                if(moveTheCursor){
                    range.setStartAfter(textNode);
                    sel.removeAllRanges();
                    sel.addRange(range);
                 }
        }
   } else if (document.selection && document.selection.createRange) {
        document.selection.createRange().text = text;
    }
  };
Answer:1

I have the following j-query code (using the jquery plugin ajax/libs/jquery/1.10.1) to move a div element to the left upon clicking on a separate div element: <script> $(document).ready(...

I have the following j-query code (using the jquery plugin ajax/libs/jquery/1.10.1) to move a div element to the left upon clicking on a separate div element: <script> $(document).ready(...

I'm writing something in meteor (in windows 8.1) and want to show a map, with the US states outlined, but the neighboring countries still showing. Ideally, the map would be an orthographic ...

I'm writing something in meteor (in windows 8.1) and want to show a map, with the US states outlined, but the neighboring countries still showing. Ideally, the map would be an orthographic ...

I understand that JavaScript automatically assigns each function/method the parameter this, and that this is bound to the object which contains it. A method is a function stored as the property of ...

I understand that JavaScript automatically assigns each function/method the parameter this, and that this is bound to the object which contains it. A method is a function stored as the property of ...

  1. invoke inner function

i am using backbonejs framework for front-end and using chartjs for charts, so to keep it simple : i have a view that renders tabs perfectly and in every tab there is a chart in it first time ...

i am using backbonejs framework for front-end and using chartjs for charts, so to keep it simple : i have a view that renders tabs perfectly and in every tab there is a chart in it first time ...