6 jQuery Cursor Functions

Share this article

jQuery code snippets to perform wonders with your mouse cursor! They can be used for setting and getting text cursor positions within input and textarea fields and selection ranges. Enjoy!

//Example jQuery get cursor position function call
$("input[name='username']").getCursorPosition();
jQuery.fn.getCursorPosition = function(){
	if(this.lengh == 0) return -1;
	return $(this).getSelectionStart();
}
//Example jQuery set cursor position function call
$("input[name='username']").setCursorPosition(5);
jQuery.fn.setCursorPosition = function(position){
	if(this.lengh == 0) return this;
	return $(this).setSelection(position, position);
}
//Example jQuery get text selection function call
$("input[name='username']").getSelection();
jQuery.fn.getSelection = function(){
	if(this.lengh == 0) return -1;
	var s = $(this).getSelectionStart();
	var e = $(this).getSelectionEnd();
	return this[0].value.substring(s,e);
}
//Example jQuery get text selection start function call
$("input[name='username']").getSelectionStart();
jQuery.fn.getSelectionStart = function(){
	if(this.lengh == 0) return -1;
	input = this[0];

	var pos = input.value.length;

	if (input.createTextRange) {
		var r = document.selection.createRange().duplicate();
		r.moveEnd('character', input.value.length);
		if (r.text == '') 
		pos = input.value.length;
		pos = input.value.lastIndexOf(r.text);
	} else if(typeof(input.selectionStart)!="undefined")
	pos = input.selectionStart;

	return pos;
}
//Example jQuery get text selection end function call
$("input[name='username']").getSelectionEnd();
jQuery.fn.getSelectionEnd = function(){
	if(this.lengh == 0) return -1;
	input = this[0];

	var pos = input.value.length;

	if (input.createTextRange) {
		var r = document.selection.createRange().duplicate();
		r.moveStart('character', -input.value.length);
		if (r.text == '') 
		pos = input.value.length;
		pos = input.value.lastIndexOf(r.text);
	} else if(typeof(input.selectionEnd)!="undefined")
	pos = input.selectionEnd;

	return pos;
}
//Example jQuery set text selection function call
$("input[name='username']").setSelection(4, 20);
jQuery.fn.setSelection = function(selectionStart, selectionEnd) {
	if(this.lengh == 0) return this;
	input = this[0];

	if (input.createTextRange) {
		var range = input.createTextRange();
		range.collapse(true);
		range.moveEnd('character', selectionEnd);
		range.moveStart('character', selectionStart);
		range.select();
	} else if (input.setSelectionRange) {
		input.focus();
		input.setSelectionRange(selectionStart, selectionEnd);
	}

	return this;
}

Frequently Asked Questions about jQuery Cursor Functions

What is the purpose of jQuery cursor functions?

jQuery cursor functions are used to manipulate the position of the cursor within an input field or a textarea. They are particularly useful when you want to set or get the cursor’s position, select a specific range of text, or move the cursor to the end of the input field. These functions can enhance the user experience by providing more interactive and intuitive input fields.

How can I use the selectionStart and selectionEnd properties in jQuery?

The selectionStart and selectionEnd properties are part of the HTML DOM and can be used with jQuery to get or set the start and end positions of the selected text in an input field or textarea. Here’s a simple example:

var input = $('#myInput');
var start = input[0].selectionStart;
var end = input[0].selectionEnd;

In this example, start will hold the index of the start of the selection and end will hold the index of the end of the selection.

What is the setSelectionRange method and how can I use it in jQuery?

The setSelectionRange method is a DOM method that sets the start and end positions of the current text selection in an input field or textarea. Here’s how you can use it with jQuery:

$('#myInput')[0].setSelectionRange(start, end);

In this example, start and end are the indices defining the new text selection.

How can I use the attribute starts with selector in jQuery?

The attribute starts with selector in jQuery is used to select elements whose attribute value begins with a specified string. Here’s an example:

$('input[name^="user"]')

This will select all input elements whose name attribute starts with “user”.

How can I move the cursor to the end of an input field in jQuery?

You can use the selectionStart and selectionEnd properties to move the cursor to the end of an input field. Here’s how:

var input = $('#myInput')[0];
input.selectionStart = input.selectionEnd = input.value.length;

This will move the cursor to the end of the input field.

How can I select a specific range of text in an input field using jQuery?

You can use the setSelectionRange method to select a specific range of text in an input field. Here’s an example:

$('#myInput')[0].setSelectionRange(start, end);

In this example, start and end are the indices defining the range of text to be selected.

Can I use jQuery cursor functions with all types of input fields?

jQuery cursor functions can be used with text fields and textarea elements. They do not work with other types of input fields like checkboxes, radio buttons, or select boxes.

How can I get the current cursor position in an input field using jQuery?

You can use the selectionStart property to get the current cursor position in an input field. Here’s how:

var cursorPosition = $('#myInput')[0].selectionStart;

This will give you the index of the cursor’s position in the input field.

Can I use jQuery cursor functions in all browsers?

jQuery cursor functions rely on DOM properties and methods that are widely supported in all modern browsers. However, older browsers may not support these features.

How can I use jQuery to insert text at the current cursor position in an input field?

You can use the selectionStart and selectionEnd properties along with the value property to insert text at the current cursor position. Here’s an example:

var input = $('#myInput')[0];
var text = 'Hello, world!';
input.value = input.value.substring(0, input.selectionStart) + text + input.value.substring(input.selectionEnd);
input.selectionStart = input.selectionEnd = input.selectionStart + text.length;

This will insert the string ‘Hello, world!’ at the current cursor position and move the cursor to the end of the inserted text.

Sam DeeringSam Deering
View Author

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

example cursor functionsjQuery
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week