it 99% working, where it is failing to add the attribute of required to the input field. when a certain condition matches and also it should run of page load even if the value of select is not changed.
and if the value of select is changed then also it should run.
Here is my Javascript code try
var e = document.getElementById("StatusID");
var strUser = e.options[e.selectedIndex].value;
var _cost = document.getElementById('_total');
if(_cost.value == 0) {
$(".Star").css('display','inline-block');
_cost.setAttribute("required", "");
}
else {
$(".Star").css('display','none');
}
You should add change event for StatusID:
var e = document.getElementById("StatusID");
e.onchange = function() {
changeHandler();
};
window.onload = function() {
changeHandler();
};
function changeHandler() {
var _cost = document.getElementById('_total');
var e = document.getElementById("StatusID");
var strUser = e.options[e.selectedIndex].value;
if(_cost.value == 0) {
$(".Star").css('display','inline-block');
_cost.setAttribute("required", "");
}
else {
$(".Star").css('display','none');
}
}
In the above (and this update), you do not need to enter the code before closing body tag.