I’m not totally sure what you’re doing, but I suspect it’s something like this:
function filter(value) {
switch (value) {
case 'number is required':
value="Number field is required";
break;
case 'something else':
value="Some other message";
break;
// More cases here
}
return value;
}
$.each(response.errors, function(i, e) {
msg = filter(e);
$("#errors").append("<div class="error-msg">" + msg + "</div>");
});
The filter
function returns the replacement message if it matches one of the cases. The caller assigns this return value to a variable, and puts it into the DIV.
Instead of a case
, you can also use an object as a dictionary, which is my preferred method:
var translations = {
'number is required': 'Number field is required',
'something else': 'Some other message',
// more cases here
};
function filter(value) {
return translations[value] || value;
}
2
solved JS switch statement inside function