Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate caret position of input and insert text on drop event
    primarykey
    data
    text
    <h2>Background</h2> <p>I am attempting to drag-and-drop a <code>&lt;div&gt;</code> element inside an <code>&lt;input&gt;</code> using the jQuery UI Draggable and Droppable components. At the moment, I have to place the caret in the position I would like the text to appear in and then drag-and-drop the <code>&lt;div&gt;</code> in to the <code>&lt;input&gt;</code>.</p> <h2>Question</h2> <p>I'm not 100% sure if this is possible to do but what I want to do is to have the caret update its position automatically as I drag the <code>&lt;div&gt;</code> between the text of the <code>&lt;input&gt;</code>. </p> <p><a href="http://jsfiddle.net/Seany84/pnRyY/" rel="nofollow">jsFiddle example here...</a></p> <h2>CSS</h2> <pre><code>.draggable { width: 250px; height: 20px; background-color: #e6eaff; border: 2px solid #3399cc; margin-bottom: 1em; padding: 4px; cursor: default; } .active { border: 2px solid #6699ff; } #droppable { font-size: 14pt; width: 400px; } #droppableHolder { margin-top: 5em; } </code></pre> <p>​</p> <h2>HTML</h2> <pre><code>&lt;div class="draggable"&gt;Text 1&lt;/div&gt; &lt;div class="draggable"&gt;Text 2&lt;/div&gt; &lt;div class="draggable"&gt;Text 3&lt;/div&gt; &lt;div class="draggable"&gt;Text 4&lt;/div&gt; &lt;div id="droppableHolder"&gt; Drop in the text box below:&lt;br /&gt; &lt;br /&gt; &lt;input type="text" id="droppable" value="INSERT ANYWHERE" /&gt; &lt;/div&gt;​ </code></pre> <h2>Javascript</h2> <pre><code>$(".draggable").draggable({ revert: true, //revert: false, helper: 'clone', start: function(event, ui) { $(this).fadeTo('fast', 0.5); //$(this).css('cursor', 'text'); //$(this).hide(); }, stop: function(event, ui) { $(this).fadeTo(0, 1); //$(this).show("explode", { pieces: 16 }, 2000); } }); $("#droppable").droppable({ hoverClass: 'active', drop: function(event, ui) { //this.value += $(ui.draggable).text(); //alert($("#droppable")); insertAtCaret("droppable", $(ui.draggable).text()); }, over: function(event, ui) { //$(this).css('cursor', 'text'); } }); function insertAtCaret(areaId,text) { var txtarea = document.getElementById(areaId); var scrollPos = txtarea.scrollTop; var strPos = 0; var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ? "ff" : (document.selection ? "ie" : false ) ); if (br == "ie") { txtarea.focus(); var range = document.selection.createRange(); range.moveStart ('character', -txtarea.value.length); strPos = range.text.length; } else if (br == "ff") strPos = txtarea.selectionStart; var front = (txtarea.value).substring(0,strPos); var back = (txtarea.value).substring(strPos,txtarea.value.length); txtarea.value=front+text+back; strPos = strPos + text.length; if (br == "ie") { txtarea.focus(); var range = document.selection.createRange(); range.moveStart ('character', -txtarea.value.length); range.moveStart ('character', strPos); range.moveEnd ('character', 0); range.select(); } else if (br == "ff") { txtarea.selectionStart = strPos; txtarea.selectionEnd = strPos; txtarea.focus(); } txtarea.scrollTop = scrollPos; } ​ </code></pre> <p><a href="http://jsfiddle.net/Seany84/pnRyY/" rel="nofollow">jsFiddle example here...</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload