5
\$\begingroup\$

I am getting list of badCatsNames from webservices, now I have two asp.net bulleted lists here,

  • Bulleted List 1 - Available Cats
  • Bulleted List 2 - Purchasing Cats

Now I need to check each bad cat name if bad cat is in available cats list, if bad cat name is then take her out of Available Cats and put in Purchasing Cats ;), but if Cat is already in purchasing Cats list then put them in Available Cats list.

If bad cat name is not in any list then ignore it.

if(badCatsNames.length > 0)
{
    for (var i = 0; i < badCatsNames.length; i++) {
        var badCatName = badCatsNames[i];

        $("[id$=listAvailableCats] li").each(function (index) {
            if ($(this).text() == badCatName) {
                $(this).appendTo($("[id$=listPurchasingCats]"));
            }
            else {
                $("[id$=listPurchasingCats] li").each(function (index) {
                    if ($(this).text() == badCatName) {
                        $(this).appendTo($("[id$=listAvailableCats]"));
                    }
                });
            }
        });
    } 
}}
\$\endgroup\$
4
  • 3
    \$\begingroup\$ Can anyone what? Please edit the title to best reflect the code's purpose. Any requests can be in the post body. \$\endgroup\$
    – Jamal
    Mar 19, 2014 at 14:50
  • \$\begingroup\$ @Jamal Done now \$\endgroup\$ Mar 19, 2014 at 14:51
  • \$\begingroup\$ Could you add the relevant HTML? \$\endgroup\$
    – RoToRa
    Mar 19, 2014 at 16:09
  • \$\begingroup\$ Checking that the length is greater than zero avails you nothing; if you iterate over an empty list, nothing happens. The test just complicates your code. \$\endgroup\$ Mar 19, 2014 at 23:45

1 Answer 1

3
\$\begingroup\$

From a once over:

  • Naming: badCatNames not badCatsNames, listPurchasingCats -> I have no clue what this means, is this a list of cats for sale ?

  • Searching on a text : please consider the contains selector: https://api.jquery.com/contains-selector/ This way you do not have to loop over every "[id$=listAvailableCats] li"

  • $("[id$=listPurchasingCats] li").each(function (index) { should not be part of the other .each() loop, are you sure this code works ?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.