I’ve got an HTML text field that I have made READONLY. Thing is though, that say the field is only a 100 pixels wide. And I have a sentence for example that doesn’t get displayed in that 100 pixels. Since it’s READONLY it’t not scrollable.
In other words. How can I still have the field not editable. But also make is so that longer strings that does not fit in the field, be viewable?
thanks!
There’s some JavaScript you can use. Unless you’re using a framework, it’d look pretty ugly though, because it isn’t trivial.
The JavaScript
keypressevent triggers when a key is pressed, but it doesn’t trigger for the cursor keys (for some reason). This is quite handy, because if you use JavaScript to prevent the default action, you’re sorted.So ideally, this would be what you need:
Just copy and paste this and it should work fine in Firefox or Chrome. The code is standards compliant, but Internet Explorer isn’t. So this won’t work in IE (except maybe versions 9 and 10… not sure about that). Also, the
classList.addbit won’t work in all but a few of the most recent versions of browsers. So we have to change these bits. First we’ll adapt thereadOnlyKeypressHandlerfunction, becauseevent.preventDefault()doesn’t work for every browser.Now to change the
classListbit.Annoyingly, addEventListener isn’t supported in IE either, so you need to make a function to handle this separately (add it above the for loop)
Then change the adding events bit:
And give the document load function a name instead of calling it in
addEventListener:And call it at the end
And once you’ve done that, it should work in all browsers.
Now just use CSS to style the
readonlyclass to take away the outline in browsers which show one: