Note that there are some explanatory texts on larger screens.

plurals
  1. POjquery text input change on button click
    primarykey
    data
    text
    <p>I gone several thread in this site and others for working fix for selectionStart and selectionEnd for ie and decided to implement code from this <a href="http://the-stickman.com/web-development/javascript/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/" rel="nofollow">site</a>.</p> <p>In simple html page there are multiple textfields with common class and unique id. There are several buttons which works as virtual key board. On focusing on any textfiled and clicking on button will place a text into that element at cursor position. If some text is selected that is also working fine in mozilla and chrome. But there is problem in other browsers</p> <blockquote> <p>opera - pacing text is fine but on selection instead replacing text it adds to selection end (i.e if selection is made from right to left then placing at left and vice versa).</p> <p>ie8 - invalid argument for line `stored_range.moveToElementText(element );</p> </blockquote> <p>whole code look like below</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt; &lt;head&gt; &lt;title&gt;selection test&lt;/title&gt; &lt;meta http-equiv="Content-type" content="text/html; charset=utf-8" /&gt; &lt;script type='text/javascript' src='js/jquery-1.4.2.js'&gt;&lt;/script&gt; &lt;script&gt; window.onload = function () { jQuery('.iptext').live('focus', function(event) { holdFocus=$(this); holdFocus=holdFocus[0];//this is required }); jQuery('.addchar').live('click', function(event) { // Get focused textfield if(holdFocus==null) { } else { var element =holdFocus; if( document.selection ){ // The current selection var range = document.selection.createRange(); // We'll use this as a 'dummy' var stored_range = range.duplicate(); // Select all text stored_range.moveToElementText( element ); // Now move 'dummy' end point to end point of original range stored_range.setEndPoint( 'EndToEnd', range ); // Now we can calculate start and end points element.selectionStart = stored_range.text.length - range.text.length; element.selectionEnd = element.selectionStart + range.text.length; } $(holdFocus).val( $(holdFocus).val().substring(0, holdFocus.selectionStart)+ $.trim($(this).text())+ $(holdFocus).val().substring(holdFocus.selectionEnd) ); } }); }; &lt;/script&gt; &lt;style type="text/css" media="screen"&gt; body { background-color: #fff; font: 16px Helvetica, Arial; color: #000; } ul {display:block;} li { list-style: none;display:inline-block;width:20px;height:20px;background-color:#808000;} ul li a {display:block;text-decoration:none; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;input type="text" id="txt1" class="iptext" autocomplete="off" value="Testing String"&gt; &lt;input type="text" id="txt2" class="iptext" autocomplete="off" value="Testing String"&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="#" class="addchar"&gt;a&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#" class="addchar"&gt;b &lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#" class="addchar"&gt;c&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#" class="addchar"&gt;d &lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>This is a fiddle <a href="http://jsfiddle.net/kiranking/bYa3h/" rel="nofollow">link</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.
    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