Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6993679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:47:42+00:00 2026-05-27T19:47:42+00:00

How I can do a foreach or similar in jQuery? I need that when

  • 0

How I can do a foreach or similar in jQuery? I need that when the user clicks in the <span>, add the class inactive to all <div>s with the vote active class in the <ul>.

I’ve tried with the .each method, but it doesn’t work.

I have this HTML:

<ul class="suggestions inactive">
    <li id="s2">
        <div class="id">2</div>
        <div class="vote inactive">
            <span class="up"></span>
            <span class="down"></span>
        </div>
        <div class="text">test2</div>
        <div class="rating">2</div>
    </li>
    <li id="s3">
        <div class="id">3</div>
        <div class="vote active">
            <span class="up"></span>
            <span class="down"></span>
        </div>
        <div class="text">test3</div>
        <div class="rating">0</div>
    </li>
</ul>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="script.js"></script>

And this script:

            $(document).ready(function(){

                var ul = $('ul.suggestions');

                // Listening of a click on a UP or DOWN arrow:

                $('div.vote span').live('click',function(){

                    var elem        = $(this),
                        parent      = elem.parent(),
                        li          = elem.closest('li'),
                        ratingDiv   = li.find('.rating'),
                        id          = li.attr('id').replace('s',''),
                        v           = 1,
                        ul2         = elem.closest('li').parent();

                    // If the user's already voted:
                    if(parent.hasClass('inactive')){
                        return false;
                    }
                    if(ul2.hasClass('inactive')){           
                        return false;
                    }

                    //If the user's already voted, add following class
                    parent.removeClass('active').addClass('inactive');                                  
                    ul2.removeClass('active').addClass('inactive');


                    if(elem.hasClass('down')){
                        v = -1;
                    }

                    // Incrementing the counter on the right:
                    ratingDiv.text(v + +ratingDiv.text());

                    // Turning all the LI elements into an array
                    // and sorting it on the number of votes:

                    var arr = $.makeArray(ul.find('li')).sort(function(l,r){
                        return +$('.rating',r).text() - +$('.rating',l).text();
                    });

                    // Adding the sorted LIs to the UL
                    ul.html(arr);

                    // Sending an AJAX request
                    $.get('ajax.php',{action:'vote',vote:v,'id':id});
                });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:47:42+00:00Added an answer on May 27, 2026 at 7:47 pm

    You don’t need .each() here at all.

    Why? Because jQuery’s selectors wonderfully select everything of a single selector type. $('p') selects every p in your HTML.

    You should use

    $(this).parent().addClass('active').removeClass('inactive');`
    

    from within the scope of the

    $('div.vote span').live('click',function() { ... } );
    

    Also, remember that .live() is deprecated in jQuery 1.7+.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In PHP I can use a foreach loop such that I have access to
In java a class can implement Iterable which lets you use the foreach() statement
I asked a similar question earlier How can I have jquery attach behavior to
I can add & remove tabs similar to the famous MSDN article . Basically
I know I can do this foreach (PropertyInfo property in myobject.GetType().GetProperties()) { if (property.DeclaringType.ToString()
How can i limit a foreach() statement? Say i only want it to run
Can someone share a simple example of using the foreach keyword with custom objects?
I can find a direct table foreign key in c# like: foreach (ForeignKey key
How can you use the following sample data either with a foreach or while
How can I increment an integer value outside the scope of a parallel.foreach loop?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.