Dynamically make a field required or recommended
April 11, 2011 by Geron Profet
This function can be used to dynamically make a field required or recommended in JavaScript. Copy the function below in the onload and call it using one the examples in the comments section.
//****************************************************
gSetFieldRequired = function (field, src, height, width){
/*
Description: This function is used to dynamically make a field required, recommended in JavaScript
Params: field = name of the field
src = url with image eg. /_imgs/frm_required.gif
height = number, optional e.g. 50
width = number optional e.g. 50
Returns: nothing
Example 1 : gSetFieldRequired(, 'new_partweight', 'required'); = add *-sign to label
Example 2: gSetFieldRequired( 'new_partweight', 'recommended'); = add +-sign to label
Example 3: gSetFieldRequired(, 'new_partweight', 'none'); = no image
Calls: nothing
Author: Geron Profet
*/
var attribute = document.getElementById(field);
if (!attribute){return};
switch(src.toLowerCase())
{
case 'required':
src = '/_imgs/frm_required.gif';
attribute.setAttribute('req', 2);
break;
case 'recommended':
src = '/_imgs/frm_recommended.gif';
attribute.setAttribute('req', 1);
break;
case 'none':
src = ''
attribute.setAttribute('req', 0);
break;
}
//if src is passed add image
if (src != '') {
var img = document.createElement("img");
img.setAttribute('src', src);
//check if custom height and width
if (height && height != '' ){img.setAttribute('height', height);}
if (width && width != '' ){img.setAttribute('height', width);}
//img.setAttribute('width', width);
var objLabel = document.getElementById(field+'_c');
if (objLabel){objLabel.appendChild(img)};
}
else{
document.getElementById(field+'_c').innerHTML = document.getElementById(field+'_c').innerText;
}
}

