Had a requirement to build a customer satisfaction survey in Sharepoint.
The survey had two fields that were to be populated by URL parameters.
There are 5 questions with 5 potential answers each (radio buttons).
Based on the a user selecting option 5 for question 1; a comments box was conditionally displayed.
Here is the code I used…
// First snippet here is to kill the Save button on the Survey form.
<script type=”text/javascript”>
_spBodyOnLoadFunctionNames.push(“setValue”);
function setValue() {
hideButton(“Save”);
}
//This function hides a button on the page
function hideButton(valueDef){
var frm = document.forms[0];
for (i=0;i< frm.elements.length;i++) {
if (frm.elements[i].type == “button” && frm.elements[i].value == valueDef) {
frm.elements[i].style.display = “none”;
}
}
}
</script>
// Second snippet uses SPUTILITY.js to grab the URL parameters and also hide/show comments field.
<script type=”text/javascript” src=”prototype.js”></script>
<script type=”text/javascript” src=”SPUtility.js”></script>
<script type=”text/javascript”>
_spBodyOnLoadFunctionNames.push(“hide”);
_spBodyOnLoadFunctionNames.push(“SetValueFromURL(‘FIELD1′, URLPARAM1’)”);
_spBodyOnLoadFunctionNames.push(“SetValueFromURL(‘FIELD2’, ‘URLPARAM2’)”);
function SetValueFromURL(fieldName,queryParamName) {
var queryParams = location.href.toQueryParams();
if (queryParams != null && queryParams[queryParamName] != null) {
SPUtility.GetSPField(fieldName).SetValue(decodeURI(queryParams[queryParamName])).MakeReadOnly();
}
}
function hide() {
SPUtility.GetSPField(‘Comments’).Hide();
}
</script>
// The tricky part… Find the Radio Buttons for Question 1 and assign OnClick event handlers. You have to view the source of your form to find these.
<script type=”text/javascript” language=”javascript”>
// Initiate InitLoad () function on the load event of your editForm.aspx
window.attachEvent(“onload”,InitLoad);
//in this function, assign event handlers to your controls
function InitLoad() {
//declaring the first radio button and attaching “onclick” event handler to it
var Q1D = document.getElementById(“ctl00_m_g_8a1be6f2_c21e_4b9b_9dac_46a7bc9aec80_ctl00_ctl01_ctl02_ctl00_ctl00_ctl04_ctl00_ctl04”);
Q1D.attachEvent(“onclick”,showFields);
var Q1C = document.getElementById(“ctl00_m_g_8a1be6f2_c21e_4b9b_9dac_46a7bc9aec80_ctl00_ctl01_ctl02_ctl00_ctl00_ctl04_ctl00_ctl03”);
Q1C.attachEvent(“onclick”,showFields);
var Q1B = document.getElementById(“ctl00_m_g_8a1be6f2_c21e_4b9b_9dac_46a7bc9aec80_ctl00_ctl01_ctl02_ctl00_ctl00_ctl04_ctl00_ctl02”);
Q1B.attachEvent(“onclick”,showFields);
var Q1A = document.getElementById(“ctl00_m_g_8a1be6f2_c21e_4b9b_9dac_46a7bc9aec80_ctl00_ctl01_ctl02_ctl00_ctl00_ctl04_ctl00_ctl01”);
Q1A.attachEvent(“onclick”,showFields);
var Q1 = document.getElementById(“ctl00_m_g_8a1be6f2_c21e_4b9b_9dac_46a7bc9aec80_ctl00_ctl01_ctl02_ctl00_ctl00_ctl04_ctl00_ctl00”);
Q1.attachEvent(“onclick”,showFields);
}
function showFields(){
var Q1D = document.getElementById(“ctl00_m_g_8a1be6f2_c21e_4b9b_9dac_46a7bc9aec80_ctl00_ctl01_ctl02_ctl00_ctl00_ctl04_ctl00_ctl04”);
if (Q1D.checked==true){
SPUtility.GetSPField(‘Comments’).Show();
// alert(‘Onlick Works’);
}
var Q1C = document.getElementById(“ctl00_m_g_8a1be6f2_c21e_4b9b_9dac_46a7bc9aec80_ctl00_ctl01_ctl02_ctl00_ctl00_ctl04_ctl00_ctl03”);
if (Q1C.checked==true){
SPUtility.GetSPField(‘Comments’).Hide();
// alert(‘Onlick Works’);
}
var Q1B = document.getElementById(“ctl00_m_g_8a1be6f2_c21e_4b9b_9dac_46a7bc9aec80_ctl00_ctl01_ctl02_ctl00_ctl00_ctl04_ctl00_ctl02”);
if (Q1B.checked==true){
// declare my metadata field “Related Documents” to be hidden using the ID from “View Source”
SPUtility.GetSPField(‘Comments’).Hide();
// alert(‘Onlick Works’);
}
var Q1A = document.getElementById(“ctl00_m_g_8a1be6f2_c21e_4b9b_9dac_46a7bc9aec80_ctl00_ctl01_ctl02_ctl00_ctl00_ctl04_ctl00_ctl01”);
if (Q1A.checked==true){
// declare my metadata field “Related Documents” to be hidden using the ID from “View Source”
SPUtility.GetSPField(‘Comments’).Hide();
// alert(‘Onlick Works’);
}
var Q1 = document.getElementById(“ctl00_m_g_8a1be6f2_c21e_4b9b_9dac_46a7bc9aec80_ctl00_ctl01_ctl02_ctl00_ctl00_ctl04_ctl00_ctl00”);
if (Q1.checked==true){
// declare my metadata field “Related Documents” to be hidden using the ID from “View Source”
SPUtility.GetSPField(‘Comments’).Hide();
// alert(‘Onlick Works’);
}
}
</script>