Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6895871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:57:50+00:00 2026-05-27T06:57:50+00:00

I’ve got an HTML text field that I have made READONLY. Thing is though,

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:57:51+00:00Added an answer on May 27, 2026 at 6:57 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
i got an object with contents of html markup in it, for example: string
I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in
I have a bunch of posts stored in text files formatted in yaml/textile (from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.