I have an application here. When you open the application then please type in a number in the textbox and click on the “Add question” button. This will add a new row and it will do a calculation between total session marks remaining and then number you have entered.
What I want to know if that how do I display in my alert that that if (‘#total-weight’) does not equal 0, then display an alert stating:
Your Total Session Marks Remaining does not equal 0.
You have '' marks remaining //this is if total session marks is above 0
OR
You need to remove '' marks // this is if total session marks is below 0
Below is code which does the subtraction:
var totalmarks = '10';
$(document).ready(function()
{
$('#total-weight').html(totalmarks);
$(document).on('change', '#qandatbl td.weight input', calculateTotal);
});
function calculateTotal()
{
var totalweight = totalmarks;
$("#qandatbl td.weight input").each(function (i, elm){
totalweight = totalweight - Number($(elm).val(), 10);
});
$("#total-weight").text(totalweight);
}
Below is the validation code where all the other alerts appear:
function validation() {
var _qid = "";
var _msg = "";
var alertValidation = "";
// Note, this is just so it's declared...
$("tr.optionAndAnswer").each(function() {
_qid = $("td.qid",this).text();
_msg = "You have errors on Question Number: " + _qid + "\n";
$(".txtWeightRow",this).each(function() {
if (!this.value) {
alertValidation += "\n\u2022 Please enter in a figure for Number of Marks for this Question\n";
}
if (alertValidation != "") {
return false; //Stop the each loop
}
});
if(alertValidation != ""){
return false;
}
});
if (alertValidation != "") {
alert(_msg + alertValidation);
return false;
}
return true;
}
I only want this alert to appear when all the rows do not contain any errors. Can this be done?
Try something like this:
http://jsfiddle.net/FVjp3/1/
This will display a message just like you wanted based on the number of marks in #total-marks.