turbostyklerx2
turbostyklerx2

Reputation: 77

How to stop adding more items with the same index in $.each loop in jQuery?

How to stop adding more items with the same index in $.each loop in jQuery?

I have this code:

var arr = data.errors;
var oneIndexIsEnough = '';

$.each(arr, function(index, value)
{
    if (value.length != 0)
    {

        if (oneIndexIsEnough != index) {
            $("#" + index).after('<span class="text-error validation-error-inline">' + value + '</span>');
            oneIndexIsEnough = index;
        } else {
            //
        }

    }
});

And the condition if (oneIndexIsEnough != index) is not working in this case.

i dont'want to add more than one error message to the same index e.g. username. How to stop adding additional error messages after the first addition and go to the next index e.g. email?

UPDATE:

I could end up with something like this:

   index      |           message                    |
-----------------------------------------------------|
   username   |  username could contain only letters |
-----------------------------------------------------|
   username   |  username is not unique              |
-----------------------------------------------------|
   username   |  username can contain only 20 char.  |
-----------------------------------------------------|
   email      |  email is not unique                 |
-----------------------------------------------------|
   email      |  email does have a bad format        |
-----------------------------------------------------|

But instead of haveing 5 error messages, I would like to have only these 2.

   username   |  username could contain only letters 

and

 email      |  email is not unique 

how to stop when the name of the index is repeating and jump to the next name(index)?

Upvotes: 0

Views: 138

Answers (2)

GPierre
GPierre

Reputation: 100

Try checking if your element already has a text-error span as its first sibling.

something like this

//I select the first sibling of your index with the class "text-error"
if (!$("#" + index).siblings('span').eq(0).hasClass("text-error")) 
{
    $("#" + index).after('<span class="text-error validation-error-inline">' + value + '</span>');
}

It should be easier to prevent multiple error message for the same element that way than keeping track of your index.

Upvotes: 0

Jaime Gomez
Jaime Gomez

Reputation: 7067

Looks to me that the problem is that you're only storing the last index you found, what if you use an array to store the processed indexes, and then check if they are already inside?

var arr = data.errors;
var processed = [];

$.each(arr, function(index, value) {
    if (value.length != 0) {
        if (processed.indexOf(value.index) == -1) {
            // process value.message
            processed.push(value.index);
        } else {
            //
        }
    }
});

This will work if the array has a layout similar to:

var arr = [
    {
        index: 'username',
        message: 'username could contain only letters'
    }
];

Upvotes: 0

Related Questions