Monday, 12 August 2013

JavaScript onchange event - SharePoint

Hello guys,
  Today also, I am going to share one interesting thing in JavaScript combined with SharePoint.

Let’s say, U r working on an Edit form. There is a Text box field(Title) and u need to do something when there is a change in the Textbox value.
How to do it????

Hmm, there is an JavaScript event to check this – “onchange”.


========================================================================
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js" type="text/javascript"></script>
<script language="javascript">

$(document).ready(function()
{

getTagFromIdentifierAndTitle("input","TextField","Title").onchange = function() {ChangeEvent()};

});


function ChangeEvent()
{

//do something
alert('hi');
}

function getTagFromIdentifierAndTitle(tagName, identifier, title) {
 var len = identifier.length;
 var tags = document.getElementsByTagName(tagName);
 for (var i=0; i < tags.length; i++) {
 var tempString = tags[i].id;
 if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
 return tags[i];
 }
 }
 return null;
}

</script>
========================================================================

Check the below post for validating different type of fields like Choice (dropdown),Lookup etc..
http://blogs.msdn.com/b/sharepointdesigner/archive/2007/06/13/using-javascript-to-manipulate-a-list-form-field.aspx

Have a good Day :)


No comments:

Post a Comment