views:

61

answers:

3

I have a bunch of forms that have a lot of <input type="text"> fields on them. The user now is requiring that I provide a "read-only" version of every single form. I would recode every field into an

<xsl:choose>
<xsl:when test="/..../permission = 'E'>
  <input ....>
</xsl:when>
<xsl:otherwise>
  ...
</xsl:otherwise>
</xsl:choose>

mess, but I'm hoping for something a little more elegant. A friend suggested

  $(function () {
        <xsl:if test="/.../permission != 'E'">
            $('input').keypress(function() { return false; });
        </xsl:if>
    });

which does part of what I want, but you can still paste into the fields and you can still delete from them.

+2  A: 

Judging from the fact that you're using XSLT, I'd say you're outputting in one of the XHTML doctypes, so why not just make the input element you're creating have the disabled attribute?

<input type="..." disabled="disabled" />

You could do this immediately in XSLT. If you're using the same input all over the place just create a template for it and use <xsl:apply-template .../> inside the test.

Post-comment edit

Apparently there's a readonly attribute as well. Silly :)

<input type="..." readonly="readonly" />
Denis 'Alpheus' Čahuk
Actually, I found that 'readonly' appears to work. I had no idea that was there.
Paul Tomblin
Hey what do you know, and it's been here all the time :) Thanks for sharing!
Denis 'Alpheus' Čahuk
A: 

I've gone the simple route:

       $(function () {
            <xsl:if test=".../permission != 'E'">
                $('input[type="text"').attr('readonly','readonly');
            </xsl:if>
        });

I don't know why I'd never heard of the "readonly" attribute before.

Paul Tomblin
One thing to note here is that if you have a reasonably large DOM, the fields will be momentarily editable until this line actually executes.
Denis 'Alpheus' Čahuk
A: 

There are differences between readonly and disabled fields- the values of readonly fields are uploaded with a form, and readonly elements can be tabbed and focused and selected, unlike disabled fields.

Also, if you change the value in straight javascript you must use 'readOnly', while html is case insensitive, and most libraries take care of it for you.

kennebec