I'm new to Javascript and struggling to make my function work for both radio buttons and text fields.
Here is the HTML code for the form:
<form action="sendmail.php" method="post" name="cascader"
onsubmit="prepareEventHandlers()" id="cascader">
<div class="TargetCenter"> <p><strong><span class="asterisk">*</span>Target Center</strong> </p>
<label>
<input type="checkbox" name="TargetCountry" value="allCountries" id="TargetCountry" />
All Countries</label>
... (more HTML code) ...
Switzerland</label>
<!-- More HTML code -->
</form>
Here is the Javascript code I used for the processTitle textfield:
function prepareEventHandlers() {
document.getElementById("cascader").onsubmit = function() {
// prevent form submission without a value in processTitle
if (document.getElementById("processTitle").value == "") {
document.getElementById("errorMessage").innerHTML = "Please enter a value";
window.scrollTo(0, 0);
document.getElementById('processTitle').style.cssText = 'background-color: #f4fc99;';
return false;
} else {
document.getElementById("errorMessage").innerHTML = "";
return true;
}
};
}
window.onload = function() {
prepareEventHandlers();
};
function checkFieldValue() {
if (document.getElementById("processTitle").value != "") {
document.getElementById('processTitle').style.cssText = 'background-color: #FFF;';
document.getElementById("errorMessage").innerHTML = "";
} else {
document.getElementById('processTitle').style.cssText = 'background-color: #f4fc99;';
}
}