Good evening, apologies for what may seem like a trivial question.
I can't seem to wrap my brain around something I am trying to accomplish. I feel it is something straightforward that I am missing.
I am trying to add multiple spans to a error div so that when an input is made and the field has lost focus, I can remove the span that showed the error to begin with.
$('.required').val('');
$('#insert').on('click', function()
{
var valid = true;
msg = '';
sp = '';
$('.required').each(function()
{
var reqs = $(this);
reqd = reqs.attr('name');
if (!reqs.val())
{
valid = false;
if (reqd == 'username')
{
reqdfn = 'ID'
}
else if (reqd == 'lastname')
{
reqdfn = 'Last Name'
}
else if (reqd == 'firstname')
{
reqdfn = 'First Name'
}
else if (reqd == 'country')
{
reqdfn = 'Country'
}
msg += reqdfn + ' cannot be blank. <br/>';
reqs.css({'border': '1px solid #C33', 'background-color': '#F6CBCA'});
}
});
if (!valid)
{
//$('#error').html('');
$('#error').slideDown();
$('#error').append('<span class="' + reqd + '">' + msg + '<span>');
}
else
{
alert('Submitted');
}
});
There is a fiddle for it here
I have tried using a for loop, but not entirely sure what I need to put in there.
I've had a Google and a look on here and from what I can see it does need a for loop, but like I say, not quite sure how to write it.
When I looked at Firebug and logged the value of reqd it was showing up as the last reqd in use.
I tried sp += reqd and obviously this gave me a value of all reqd's together as I did expect.
I am just stumped as to where I am going wrong.
Any pointers would be greatly appreciated.
Thanks.