Tuesday, September 20, 2011

Enable/Disable Fields using JQuery in NewForm and EditForm.aspx

Hide  fields
$("td.ms-formlabel:contains('Type or Location')").parent().hide();
----------------------------------------

<script type="text/javascript"
src="/root folder/customization/js/jquery-1.2.6.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var text = $("select[title$='Quotes Attached'] :selected").text();
if(text != "Yes")
{
$("input[value$='OK']").attr('disabled', true);
}

$("select[title$='Quotes Attached']").change(function()
{
var text = $("select[title$='Quotes Attached'] :selected").text();
if(text == "Yes")
{
//alert('Please attach the Quote by clicking Attach File option');
$("input[value$='OK']").attr('disabled', false);
}
else
{
$("input[value$='OK']").attr('disabled', true);
}
});

});
</script>


------------------------------------------------------------------------------
Please read the below Article

How to Enable/Disable Input element in JQuery

To disable input elements (checkbox, radio button, text box, button etc) in jquery, you need to set disabled attribute.
// check box
$("#check_box_id").attr("disabled", "disabled");
// radio button
$("#radio_btn_id").attr("disabled", "disabled");
// Text box
$("#text_box_id").attr("disabled", "disabled");
// button
$("#btn_id").attr("disabled", "disabled");
To Enable the input elements (checkbox, radio button, text box, button etc) , we need to remove disabled attribute. This can be done in the following way.
// check box
$("#check_box_id").removeAttr("disabled");
// radio button
$("#radio_btn_id").removeAttr("disabled");
// Text box
$("#text_box_id").removeAttr("disabled");
// button
$("#btn_id").removeAttr("disabled");

May times there are requirements like we need to define a field as a required field in SharePoint. Yes, you will say what is new with that? This is built in feature of SharePoint.

Ok, what if I say that field should be mandatory while updating list item but not while inserting the list item. What if I say if value of one drop down in xyz, abc field becomes mandatory?

Well, in scenarios like this we can use jQuery. Yes, jQuery is the answer to this question. Let us put this in to a real scenario.

Ok, before getting into this, I would like to share some steps about this. First we need to handle the change event of drop down. Then we will check the value of the drop down, if the value is suspended, we will pop up one message saying that now because you have selected Suspend status, you must mention the reason for this and we will disable the button OK. We have to do this because there is no way we can stop user clicking ok button unless user writes the reason. Otherwise item will be updated which we do not want.

we also need to handle lost focus event of text area, and at the end we will count the number of characters in the text area. If total comes to zero, well then keep button disabled. If it is not zero and more than zero, then we will enable the button.

Or else we can ask user to select Status back to value which was earlier and enable the button.

Yes, this all steps are required. Try for yourself and you will realize that yes these all steps are required. It may look like some lengthy solution, But remember we did all this stuff right on browser. We never went to write down the code in event handler (this stuff really annoy the end user, as it takes to a different error page, or creating any custom field for this. This is what we wanted and this is the power of jQuery. Isn’t it.

Ok, let us see now in practical

First add the content editor web part above the New Form or Edit Form LVWP where ever you want to place the logic.

This is the layout of my editform.



Add the following code to the content editor web part. Do not forget to reference appropriate jquery file from your library.

<script type="text/javascript" src="{site URL}/Shared%20Documents/jquery-latest.js">
</script>



$(document).ready(function() {
//Below function makes sure that if the status is not suspended in edit mode, it disables the reason text //area
var text = $("select[title$='Status'] :selected").text();
if(text != "Suspended")
{
$("textarea[title$='Reason'] ").attr('disabled', true);

}


Below function makes sure that when the status drop down is changed to suspended status, reason textarea becomes enabling to write down the result and disabled the OK button unless user enters the reason. If suspend is not selected, again disable the reason field and enable the OK button.

$("select[title$='Status']").change(function()
{

var text = $("select[title$='Status'] :selected").text();

if(text == "Suspended")
{
alert('you must provide reason for suspending this order');
$("input[value$='OK']").attr('disabled', true);
$("textarea[title$='Reason'] ").attr('disabled', false);
}
else
{
$("input[value$='OK']").attr('disabled', false);
$("textarea[title$='Reason'] ").attr('disabled', true);
}

});


Finally below is the blur event of the reason field, which checks if anything is entered in reason field or not. If not keep the button OK disabled, or else if something is written enable the button.

$("textarea[title$='Reason']").blur(function()
{

var text = $("textarea[title$='Reason'] ").text();

if(text.length <= 0)
{
$("input[value$='OK']").attr('disabled', true);
}
else
{
$("input[value$='OK']").attr('disabled', false);
}


});

});

</script>


So here is the result of it.

See below image as status is not suspended I am not able to type in anything in reason field.



Below figure shows when I changed the status to Suspended, it pops up message and disabled the ok button after message is displayed.






After mentioning the reason, OK button enables.



That is it. You have just made required field validation without using any custom field or writing event handler.

5 comments:

  1. hi there , I tried my best to do this but i was not successful. What version of jquery library are you using and does it make any difference

    ReplyDelete
    Replies
    1. Are you using 2010 or Moss 2007? You need to have jquery-1.2.6.min.js or higher version of file in the specific library and refer that path in your code and will work ...Good luck

      Delete
  2. Hi, what will be scrpit to disable in edit form OK (to submit) and prevent further edit item if some specific criteria is met (example: Status= Approved)

    ReplyDelete
  3. Hi Mala, Can you please share if have achieved it ?

    ReplyDelete
  4. Hi,
    Place if condition with that Status to prevent the further edit item

    ReplyDelete