I'm a bit clumsy at writing JavaScript. I have some nice code like this:
(function($){
$.fn.forceNumeric = function(){
return this.each(function(){
$(this).keyup(function(){
if (!/^[0-9]+$/.test($(this).val())){
$(this).val($(this).val().replace(/[^0-9]/g, ''));
}
});
});
};
})(jQuery);
I know what it does and how it works. I can call the function and I see why it works.
For the life of me, I cannot figure out how to use that same elegant style/approach for my Neanderthal function:
function showHideErrorBox(theFriendlyName, check){
var topLevelDiv,
numDivsAfter;
if(check === 0){
// REMOVE THE OLD ERROR MESSAGE IN THE ERROR BOX
$('.form-validation-errors').find('div').each(function(){
if( $(this).html().indexOf(theFriendlyName) != -1 ){
$(this).remove();
}
});
}
// CHECK WHETHER THE ENTIRE MESSAGE BOX CAN BE REMOVED NOW
topLevelDiv = document.getElementById('alert-form-errors');
numDivsAfter = topLevelDiv.getElementsByTagName('div').length;
if(check === 0){
if(numDivsAfter <= 1){
$('#alert-form-errors').hide();
}
}else if(check === 1){
if(numDivsAfter >= 1){
$('#alert-form-errors').show();
}
}
}
Can anyone help me out? Where do I start? I have Google'd for tutorials, but I have not been able to find something suitable for my needs.