Monday, July 31, 2017

Have checkbox set and disable a metadata field

Here's a snippet of code you can throw into the NewForm.aspx page if you need a checkbox to set a value in a metadata field and disable that metadata field:

 //Execute the Query.
$(document).ready(function(){
// Get the value of the checkbox
if ($('input[name="NewDepartment"]:checked').val() == "true")
{
boolAutoApprove = true;
}
else 
{
boolAutoApprove = false;
}

// When checkbox is clicked, set or unset the value of the Department field
$("input[id ^= 'NewDepartment']").change(function(){
boolAutoApprove = !boolAutoApprove;

if (boolAutoApprove) 
{
$("div[title='Department']").children().prop("contenteditable",false).css('background-color','#E0E0E0');
$("div[title='Department']").parent().find('img').hide();
$("div[title='Department']").children().html("New Department");
}
else 
{
$("div[title='Department']").children().prop("contenteditable",true).css('background-color','#FFFFFF');
$("div[title='Department']").parent().find('img').show();
$(".ms-taxonomy-writeableregion").html("");
}
});

});

No comments: