Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's some JavaScript you can use. Unless you're using a framework, it'd look pretty ugly though, because it isn't trivial.</p> <p>The JavaScript <code>keypress</code> 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.</p> <p>So ideally, this would be what you need:</p> <pre><code>// 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 &lt; 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); } }); </code></pre> <p>Just copy and paste this and it should work fine in Firefox or Chrome. The code is <em>standards compliant</em>, but Internet Explorer isn't. So this won't work in IE (except maybe versions 9 and 10... not sure about that). Also, the <code>classList.add</code> 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 <code>readOnlyKeypressHandler</code> function, because <code>event.preventDefault()</code> doesn't work for every browser.</p> <pre><code>function readOnlyKeypressHandler (event) { if (event &amp;&amp; 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; } </code></pre> <p>Now to change the <code>classList</code> bit.</p> <pre><code> // 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'; } </code></pre> <p>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)</p> <pre><code>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; } } </code></pre> <p>Then change the adding events bit:</p> <pre><code> addEvent(readOnlyInputs[i], 'click', readOnlyClickHandler); addEvent(readOnlyInputs[i], 'blur', readOnlyBlurHandler); addEvent(readOnlyInputs[i], 'keypress', readOnlyKeypressHandler); </code></pre> <p>And give the document load function a name instead of calling it in <code>addEventListener</code>:</p> <pre><code>function docLoadHandler () { ... } </code></pre> <p>And call it at the end</p> <pre><code>addEvent(document, 'load', docLoadHandler); </code></pre> <p>And once you've done that, it should work in all browsers.</p> <p>Now just use CSS to style the <code>readonly</code> class to take away the outline in browsers which show one:</p> <pre><code>.readonly:focus { outline: none; cursor: default; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload