Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a prototype for a input element?

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?

like image 875
BrunoLM Avatar asked Nov 17 '10 17:11

BrunoLM


People also ask

What is prototype in JavaScript?

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, ...

What makes a good input form prototype?

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.

Is it possible to add htmlelement extensions to a prototype?

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:

Can I change the prototype of an object?

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:


2 Answers

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?

like image 95
Tim Down Avatar answered Oct 02 '22 20:10

Tim Down


You'll want to extend the HTMLInputElement interface, like this:

HTMLInputElement.prototype.selectionStart = …

However, JavaScript experts consider this a bad practice.

like image 38
Marcel Korpel Avatar answered Oct 02 '22 20:10

Marcel Korpel