0

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.

2 Answers 2

1

I believe I understand...

Just move the $('#error').append('<span class="' + reqd + '">' + msg + '<span>'); into the field error logic. That way each field that has an error will append a new span.

Then you can still $('#error').slideDown(); at the bottom.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to put appending into each. Also I reccomend make an object, were key will be a name (from input) and value will be human-name. It will shorten your code.

$('.required').val('');
$('#insert').on('click', function(){
    var valid = true,
        msg = '',
        sp = '';

    var names = {
        'username' : 'ID',
        'lastname' : 'Last Name',
        'firstname' : 'First Name',
        'country' : 'Country'
    };

    $('.required').each(function() {
        var reqs = $(this);
        reqd = reqs.attr('name');

        if (!reqs.val()) 
        {
            valid = false;
            reqdfn = names[reqd];

            msg += reqdfn + ' cannot be blank. <br/>';
            reqs.css({'border': '1px solid #C33', 'background-color': '#F6CBCA'});

            $('#error').append('<span class="' + reqd + '">' + msg +  '<span>');
        }
    });

    if (!valid) {
        //$('#error').html('');
        $('#error').slideDown();

    } else {
        alert('Submitted');
    }
});

1 Comment

Hi Dasnny, thanks for answering, when I try it this way, I am getting multiple spans in the div as follows: ID, ID + Firstname, ID + First Name + Last Name, ID + First Name + Last Name + Country. It seems to just be iterating through each one and shoving it in there as a span. Not quite sure where i'm going wrong. Oh well. Thanks anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.