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());
}

Advertisements

About Joseph Velliah
As a SharePoint Developer my professional interests tend to be technical and SharePoint focused. I run a blog at "SP RIDER" where you can expect to read HOW TOs and scenarios that I run into during my day to day job. I hope my posts will give back a little to the community that is helped me.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: