<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>new function($) {
$.fn.getCursorPosition = function() {
var pos = 0;
var el = $(this).get(0);
// IE Support
if (document.selection) {
el.focus();
var Sel = document.selection.createRange();
var SelLength = document.selection.createRange().text.length;
Sel.moveStart('character', -el.value.length);
pos = Sel.text.length - SelLength;
}
// Firefox support
else if (el.selectionStart || el.selectionStart == '0')
pos = el.selectionStart;
return pos;
}
} (jQuery);
$(document).ready(function(){
$("#butt").click(function(){
alert($("#txt").getCursorPosition());
});
});
</script>
<input type="text" id="txt"><button id="butt">Get Position</button>
Thanks http://stackoverflow.com
$.fn.getCursorPosition = function() {
var pos = 0;
var el = $(this).get(0);
// IE Support
if (document.selection) {
el.focus();
var Sel = document.selection.createRange();
var SelLength = document.selection.createRange().text.length;
Sel.moveStart('character', -el.value.length);
pos = Sel.text.length - SelLength;
}
// Firefox support
else if (el.selectionStart || el.selectionStart == '0')
pos = el.selectionStart;
return pos;
}
} (jQuery);
$(document).ready(function(){
$("#butt").click(function(){
alert($("#txt").getCursorPosition());
});
});
</script>
<input type="text" id="txt"><button id="butt">Get Position</button>
Thanks http://stackoverflow.com
Comments
Post a Comment