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 6781717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:39:25+00:00 2026-05-26T16:39:25+00:00

I have a button that when clicked, should only execute its code if the

  • 0

I have a button that when clicked, should only execute its code if the number of div that have the class of “like” is greater than 0. The problem is that even if this is true, the button still won’t execute its code. Note that I have tried both .length and .size() to the same result.

$(document).ready(function() {
countlikes = $('[id^=post_].like').length;
likestatus = 1;
$('#show_likes').on('click', function() {
    if (countlikes >0) {
        likestatus++;

        $('[id^=post_].like').toggle();

        if (likestatus % 2 == 0) {
            $('#hidelikedbtn').removeClass('hidelikedimgoff').addClass('hidelikedimgon');
        } else {
            $('#hidelikedbtn').removeClass('hidelikedimgon').addClass('hidelikedimgoff');
        }

    return false;

    } else {
        return false;
    }
});
});

Any ideas?

  • 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-26T16:39:26+00:00Added an answer on May 26, 2026 at 4:39 pm

    Assuming that the number of elements with “like” class varies after the page loads, the problem is that you currently assign a value to countlikes only once, outside your click handler, so every time the handler runs it will be using the original countlikes value and not a current count. Move the assignment inside the handler and it should work. (Though given the variable is only actually used in one place at the moment you don’t really need it, you can just test $('[id^=post_].like').length directly in your if statement.)

    Also your question says “greater than 1” and your code says “> 0”, so even if it worked it wouldn’t be doing what you describe.

    Also, unrelated to your problem, the last else case is redundant: at the end of the if you return false, and in the else you return false, so why not just move the return false; outside the if and delete the else?

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

Sidebar

Related Questions

I have a usercontrol that hides a div when a button is clicked. <asp:LinkButton
I have a button that depending on a class value should send different values
I have a button that when clicked will run a stored procedure on a
In my Silverlight UI, I have a button that when clicked pops up a
In my Delphi Project i want to have a 'Settings' button that when clicked,
I have a button that opens a dialog when clicked. The dialog displays a
I have a form in C# that has a button that, when clicked, I
I have a button that I would like to disable when the form submits
I have a list like this: <ul> <li id=adm-thumb onclick=javascript:addBanner('bowling.jpg');> <div class=adm-tick></div> <img src=img/banners/bowling.jpg
I need to show a particular div when a button is clicked, only if

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.