8

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!

  • 1
    What do you mean as field? http://jsfiddle.net/hbKBD/ For example, textbox is scrollable))) – Chuck Norris Nov 29 '11 at 09:05
  • yes exactly like your example. But check, sure the thing is not editable, and that's great. But a piece of your string is getting cut off because the textfield is too short to display the entire String. How do you fix that? In your example I can't scroll etc. –  Nov 29 '11 at 09:11
  • You can scroll it with mouse... just select the text – Chuck Norris Nov 29 '11 at 09:12
  • Although it's possible to scroll with a mouse, keyboard accessibility needs to be considered. A text field that isn't readonly/disabled is fully accessible with the keyboard, but that's no longer the case when it becomes disabled/readonly – Shiraz Jul 13 '20 at 10:00

6 Answers6

4

Do not make the textarea readonly. This is what I did:

<textarea id="mytextarea" wrap="off" style="overflow:auto"></textarea>

,then in your JavaScript, using jQuery:

$('#mytextarea').keypress(function(event){
    event.preventDefault();
});
vinceg
  • 81
  • 3
4

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 keypress event 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:

// get all readonly inputs
var readOnlyInputs = document.querySelectorAll('input[readonly]');

// Function to fire when they're clicked
// We would use the focus handler but there is no focus event for readonly objects
function readOnlyClickHandler () {
    // make it not readonly
    this.removeAttribute('readonly');
}
// Function to run when they're blurred (no longer have a cursor
function readOnlyBlurHandler () {
    // make it readonly again
    this.setAttribute('readonly');
}
function readOnlyKeypressHandler (event) {
    // The user has just pressed a key, but we don't want the text to change
    // so we prevent the default action
    event.preventDefault();
}
// Now put it all together by attaching the functions to the events...

// We have to wrap the whole thing in a onload function.
// This is the simplest way of doing this...
document.addEventListener('load', function () {
    // First loop through the objects
    for (var i = 0; i < readOnlyInputs.length; i++) {
        // add a class so that CSS can style it as readonly
        readOnlyInputs[i].classList.add('readonly');
        // Add the functions to the events
        readOnlyInputs[i].addEventListener('click', readOnlyClickHandler);
        readOnlyInputs[i].addEventListener('blur', readOnlyBlurHandler);
        readOnlyInputs[i].addEventListener('keypress', readOnlyKeypressHandler);
    }
});

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.add bit 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 the readOnlyKeypressHandler function, because event.preventDefault() doesn't work for every browser.

function readOnlyKeypressHandler (event) {
    if (event && event.preventDefault) {
        // This only runs in browsers where event.preventDefault exists,
        // so it won't throw an error
        event.preventDefault();
    }
    // Prevents the default in all other browsers
    return false;
}

Now to change the classList bit.

    // add a class so that CSS can style it as readonly
    if (readOnlyInputs[i].classList) {
        readOnlyInputs[i].classList.add('readonly');
    } else {
        readOnlyInputs[i].className += ' readonly';
    }

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)

function addEvent(element, eventName, fn) {
    if (element.addEventListener) {
        element.addEventListener(eventName, fn, false);
    } else if (element.attachEvent) {
        // IE requires onclick instead of click, onfocus instead of focus, etc.
        element.attachEvent('on' + eventName, fn);
    } else {
        // Much older browsers
        element['on' + eventName] = fn;
    }
}

Then change the adding events bit:

    addEvent(readOnlyInputs[i], 'click', readOnlyClickHandler);
    addEvent(readOnlyInputs[i], 'blur', readOnlyBlurHandler);
    addEvent(readOnlyInputs[i], 'keypress', readOnlyKeypressHandler);

And give the document load function a name instead of calling it in addEventListener:

function docLoadHandler () {
    ...
}

And call it at the end

addEvent(document, 'load', docLoadHandler);

And once you've done that, it should work in all browsers.

Now just use CSS to style the readonly class to take away the outline in browsers which show one:

.readonly:focus {
    outline: none;
    cursor: default;
}
Nathan MacInnes
  • 11,033
  • 4
  • 35
  • 50
  • Doesn't this code make the read only field editable once clicked, by removing the readonly attribute? – Shiraz Jul 14 '20 at 09:06
  • 1
    Yes, but prevents keyboard input (albeit not 100% robustly - users can still paste for example). This is 9 years old, and so the short version should now work! – Nathan MacInnes Jul 15 '20 at 13:55
2

if http://jsfiddle.net/msmailbox/jBGne/ this is what you needed then you can try this with div.

<div id="alo">sfbskdgksfbgkjsfngndflgndfgldfngldfgldfg</div>

with css

#alo
{
    width:100px;
    overflow-x:scroll;
    overflow-y:hidden;
}
M S
  • 3,995
  • 3
  • 25
  • 36
1

The CSS property overflow sets the scroll behaviour, and for example overflow: auto only display scrollbars when the content extends beyond the area. With overflow: scroll you get the scrollbars every time.

Viruzzo
  • 3,025
  • 13
  • 13
  • It's true what you are saying. But Horizontal scroll bars on textfield would look pretty ugly from a UI design point of view. Is there not something I can do that the user can atleast click inside the textfield. And scroll left and right with their cursor using the arrow buttons on their keyboard. But also make it to not be editable? –  Nov 29 '11 at 09:08
  • Why are scrollbars worse than non-UI solutions? Having the capability to scroll without the bars is counterintuitive. – Viruzzo Nov 29 '11 at 09:44
1

Use a Textarea instead of a input textfield. U cant add scroll to a textfield

<textarea cols="50" rows="5" ></textarea>

Pavan Jain
  • 41
  • 3
0

Define height for your container div of text and use overflow:auto something like below css code.

.yoruclass{
width:100px;
height:100px;/* stop text to go beyond the define height */
overflow:hidden;/* making text div scrollable */
}
Code Lover
  • 8,099
  • 20
  • 84
  • 154