From: Tim Düsterhus Date: Sat, 20 Oct 2012 11:44:02 +0000 (+0200) Subject: Add setCaret to WCF.js X-Git-Tag: 2.0.0_Beta_1~836^2~9 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=f73ff47d4b7ffef6a4a0e354fe32a58bd368087c;p=GitHub%2FWoltLab%2FWCF.git Add setCaret to WCF.js --- diff --git a/wcfsetup/install/files/js/WCF.js b/wcfsetup/install/files/js/WCF.js index a86a5ebcf4..7f9249beec 100755 --- a/wcfsetup/install/files/js/WCF.js +++ b/wcfsetup/install/files/js/WCF.js @@ -338,6 +338,42 @@ $.fn.extend({ return $position; }, + /** + * Sets the caret position of current element. If the element + * does not equal input[type=text], input[type=password] or + * textarea, false is returned. + * + * @param integer position + * @return boolean + */ + setCaret: function (position) { + if (this.getTagName() == 'input') { + if (this.attr('type') != 'text' && this.attr('type') != 'password') { + return false; + } + } + else if (this.getTagName() != 'textarea') { + return false; + } + + var $element = this.get(0); + + // set focus to enable caret on this element + this.focus(); + if (document.selection) { // IE 8 + var $selection = document.selection.createRange(); + $selection.moveStart('character', position); + $selection.moveEnd('character', 0); + $selection.select(); + } + else if ($element.selectionStart || $element.selectionStart == '0') { // Opera, Chrome, Firefox, Safari, IE 9+ + $element.selectionStart = position; + $element.selectionEnd = position; + } + + return true; + }, + /** * Shows an element by sliding and fading it into viewport. *