I have a input element:
<input type="text" id="t" value="abcdefghij" />
I want to create a selectionStart
document.getElementById("t").selectionStart
The functions I need are:
function GetSelectionStart(o) {
if (o.createTextRange) {
var r = document.selection.createRange().duplicate()
r.moveEnd('character', o.value.length)
if (r.text == '') return o.value.length
return o.value.lastIndexOf(r.text)
} else return o.selectionStart;
}
function GetSelectionEnd(o) {
if (o.createTextRange) {
var r = document.selection.createRange().duplicate()
r.moveStart('character', -o.value.length)
return r.text.length
} else return o.selectionEnd;
}
How can I add this "property" to <input type="text" />
on IE? Is it possible?
Prototype in JavaScript. JavaScript is a prototype based language, so, whenever we create a function using JavaScript, JavaScript engine adds a prototype property inside a function, Prototype property is basically an object (also known as Prototype object), where we can attach methods and properties in a prototype object, ...
Input form that can be typed with real text using keyboard and have a state on it, also have a condition is even better. This seems like a very basic table stakes item. You have to have this functionality if you want to call it a “prototype” - Sometimes what a user might input into the field is germane to the usability test.
In browsers that support adding methods to prototype of native objects such as HTMLElement all DOM extensions on the element are available by default without ever having to call Element.extend (), dollar function or anything! This will then work in those browsers:
prototype is a property available with all JavaScript objects. You are not advised to change the prototype of an object that you do not control. Only change the prototype of your own objects. The JavaScript prototype property allows you to add new properties to objects:
First, it's a bad idea both in principle and in practice to try extend host objects. Host objects such as DOM elements can do pretty much what they like; in particular, they are not obliged to support what you're trying to do and in IE <= 8, which is what you're targetting with this code, DOM elements simply don't support this. Your options are either to use a function to which you pass an input element, or create a wrapper object for each input that has the methods and properties you need.
Second, your GetSelectionStart()
and GetSelectionEnd()
functions are flawed: they will not handle new lines correctly in textareas and have faulty logic around lastIndexOf
(what if the selected text appears more than once in the input?). I've done quite a lot of work on this, and have come up with what I'm fairly convinced is the best function around for getting input and textarea selections in all major browsers, which I last posted here a couple of days ago: Is it possible to programmatically detect the caret position within a <input type=text> element?
You'll want to extend the HTMLInputElement
interface, like this:
HTMLInputElement.prototype.selectionStart = …
However, JavaScript experts consider this a bad practice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With