Need Onchange events for SharePoint:FormFields

terahex picture terahex · Jun 4, 2012 · Viewed 10.2k times · Source

I need to call a javascript function when a SharePoint:FormField has changed. This is to provide some client side feedback about there selection.

I need to add this on a new content type form, in my Visual Studio project. The SharePoint:FormField doesn't appear to have the Onchange event, like normal ASP controls.
I looked at ValueChanged event, but it looks like read only.

Any suggestions or help would be appreciated

Answer

terahex picture terahex · Jun 7, 2012

Solution Found:

I resolved the problem thanks to Google and webborg.blogspot.com/2008/04/… and mysplist.blogspot.com/2010/03/…. I would add that the JavaScript should be inline and after following your SharePoint:FormField

<table cellpadding="0" cellspacing="0" id="onetIDListForm" style="width:100%">
<tr>
<td width="400px"  valign="top"  class="ms-formbody"> 
    <SharePoint:FormField runat="server" ID="MyGender" ControlMode="New" FieldName="MyGender" />
    <SharePoint:FieldDescription runat="server"  ID="field_MyGender_Description"  FieldName="My Gender" ControlMode="New"  /> 
</td>
</tr>
</table> 

<script type="text/javascript"> 
function getField(fieldID) 
{ 
    var docTags = document.all;
    for (var i=0; i < docTags.length; i++) {
        if (docTags[i].title == fieldID) { 

            return docTags[i] 
        } 
    } 
} 

function DisplayMessage()
{
    if (getField('My Gender').value == 'Male') {
     alert('Hello Manhood');
     }
}

getField('My Gender').onchange = function() {DisplayMessage()};

</script>