1

It's a bit moot point but I'm still curious why the two following lines aren't equivalent.

$("#boink").selectionStart = 3;
document.getElementById("boink").selectionStart = 3;

The working example comes from this question. For some reason, the jQuery fetched control doesn't have the property selectionStart nor selectionEnd.

What should be used instead if I'm still going to apply jQuery selectors?

Community
  • 1
  • 1
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438

3 Answers3

4

That's because $("#boink") is a jQuery object and not the DOM element itself. In order to make those 2 lines equivalente you should do

$("#boink")[0].selectionStart = 3;

The array contains all matched DOM elements. Since you're selecting by id, one element can match as most so you just pick the first one using [0]

Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
2

You can always get the original DOM element using

$("#boink")[0].selectionStart = 3;
U.P
  • 7,357
  • 7
  • 39
  • 61
2

$("#boink") will return jQuery object and not the control itself. To get the control you have to take element with .get method or using [] notation:

$("#boink")[0].selectionStart = 3;
$("#boink").get(0).selectionStart = 3;
Igor Dymov
  • 16,230
  • 5
  • 50
  • 56