Well, you are struggling with browser restrictions and events.
You cannot simulate a UI event in such a way that it imitates human interaction (this is a security issue). However, there are some built-in ways to control text in multiple DOM elements. The textarea element is a good example of this with its selectStart / selectionEnd variables (specific browser version). You should probably note that in Firefox, selectStart / selectionEnd does the desired effect, but, again, only under Firefox.
So, your problem cannot be solved in a cross-browser way. You will have to go to simulate the effect by slicing text, etc.
Here is a quick pseudo-code example of what I mean:
element.onblur = function (event) { this.hidden = this.value; if (this.value.length > (this.offsetsetWidth / 12)) { this.shown = this.value.slice(this.value.length - (this.offsetWidth / 12)); this.value = this.shown; } }; element.onfocus = function (event) { this.value = this.hidden || this.value; };
Here, the section of the fragment is based on the ratio of the width of the monospaced font (12px) to the width of the element (whatever it may be). So, if we have a rectangle with a size of 144 pixels, and we are dealing with a monospaced font, we know that we can put 12 characters in the field at a time, so we cut it this way.
If the length of the input value is 24 characters, we do simple math to cut in the line (24 - (w / 12)) to the position.
All this hack . And honestly, is there any practical application to this? Perhaps you should add subtext under the tab, which gives an ellipse-like preview of the last part of the text.
Justin van horne
source share