Wednesday, January 5, 2011

Using jQuery to Make a Field Read Only in SharePoint

So,let’s say you have a field on a SharePoint Form and you want to make it read only. You COULD just open it up in SPD and easily make it read only, but some people are purists and don’t like use SPD or modify the default new/edit/disp forms. I try to avoid modifying these forms and it seemed like such a simple task that I didn’t want to create a new un-ghosted form.  So how do you do it?

Anyway, here’s the entire script:


<script src="jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">

jQuery(document).ready(function($){

     //capture keypress on our read only field and return false
     $('#idOfInputField').keypress(function() {
            return false;
     });

})

</script> 

You can find the ID of your input field by viewing the source, this ID stays consistent as long as you don’t muck with the list or form in the wrong way.  Please note, you CANNOT disable the input field as an alternative to capturing the keypress. If you do this and save the form, any data in the disabled fields will be wiped out.

No comments:

Post a Comment