Load / Set Value on change of SharePoint Drop Down using client script

Do you sometimes wish to make some special form validation or other javascript actions on SharePoint form fields (like for example onchange, onfocus, onblur, etc?). You can add special functions on events to the SharePoint form fields using JavaScript. Use the following codes to do the same;

// The below line is to set onchange event for the dropdown

_spBodyOnLoadFunctionNames.push(“SetDropDownEvent”);

function SetDropDownEvent()
{
getField(‘select’,’Category’).onchange = function() {syncDropDowns()};
}

function getField(fieldType,fieldTitle)
{
var docTags = document.getElementsByTagName(fieldType);
for (var i=0; i < docTags.length; i++)
{
if (docTags[i].title == fieldTitle)
{
return docTags[i]
}
}
}

function syncDropDowns()
{
itemId = getField(‘select’,’Category’).options[getField(‘select’,’Category’).selectedIndex].value;
getSetListItem();
}

var itemId;
var listItem;
var list;
var clientContext;
var value;

function getSetListItem()
{

this.clientContext = SP.ClientContext.get_current();

if (this.clientContext != undefined && clientContext != null)
{
var webSite = clientContext.get_web();
this.list = webSite.get_lists().getByTitle(“Category Config”);
this.listItem = list.getItemById(itemId);
clientContext.load(this.listItem);
this.clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess),
Function.createDelegate(this, this.OnLoadFailed));
}
}

function OnLoadSuccess(sender, args)
{
value = this.listItem.get_item(“Status”);
alert(value);
}

function OnLoadFailed(sender, args)
{
alert(‘Request failed. ‘ + args.get_message() + ‘\n’ + args.get_stackTrace());
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.