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

  • Home
  • SEARCH
  • 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 1083699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:28:09+00:00 2026-05-16T22:28:09+00:00

So I am making a voting system, basically a Thumbs Up & Thumbs Down

  • 0

So I am making a voting system, basically a Thumbs Up & Thumbs Down voting system. I am using CakePHP and jQuery with MySQL but want to make sure the front end is correct and this is the best way to do it.

I want the user to be able to change their vote, so utilizing jQuery is this the best and most efficient way? I’m fairly novice with jQuery when it comes to class manipulation.

the ID field is going to be the unique id of the photo the users will be voting on. This of course is just a test and this is not going to be the final product in production. There will be multiple photos on the page and the user votes Up or Down for each of them.

Here is the code.

<?php
echo $javascript->link('jquery/jquery-1.4.2.min',false);
?>
<script type="text/javascript">
    $(document).ready(function() {
        $('.vote').click(function () {
            if ($(this).hasClass("current")) {
                alert("You have already voted for this option!");

                return;
            }
            var parentId = $(this).parent("div").attr("id");
            if ($(this).hasClass("up")) {
                //Do backend query and checking here
                alert("Voted Up!");
                $(this).toggleClass("current");
                if ($("#" + parentId + "-down").hasClass("current")) {
                    $("#" + parentId + "-down").toggleClass("current");
                }
            }
            else if($(this).hasClass("down")) {
                //Do backend query and checking here
                alert("Voted Down!");
                $(this).toggleClass("current");
                if ($("#" + parentId + "-up").hasClass("current")) {
                    $("#" + parentId + "-up").toggleClass("current");
                }
            }



        });
    });
</script>
<div id="1234">
    <a id="1234-up" class="vote up" >+</a> | <a id="1234-down" class="vote down" >-</a>
</div>
  • 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-16T22:28:09+00:00Added an answer on May 16, 2026 at 10:28 pm

    You could do it this way:

    <script type="text/javascript">
        $(document).ready(function() {
            $('.vote').click(function () {
                if ($(this).hasClass("current")) {
                    alert("You have already voted for this option!");
                } else {
                    var parentId = $(this).parent("div").attr("id");
                    var error = false;
    
                    if ($(this).hasClass("up")) {
                        //Do backend query and checking here (SET: error)
                        alert("Voted Up!");
                    }
                    else if($(this).hasClass("down")) {
                        //Do backend query and checking here (SET: error)
                        alert("Voted Down!");
                    }
                    //removes all the votes 
                    $(this).parent("div").children().removeClass("current");
    
                    if(!error) {
                        $(this).addClass("current");
                    } else {
                        alert("There was an error");
                    }
                }
                return false;
            });
        });
    </script>
    <div id="1234">
        <a class="vote up" href="#">+</a> | <a class="vote down" href="#">-</a>
    </div>
    

    It removes the need for extra html id’s and you dont need the doubled up code to add the current class. I’m not sure which is quicker, but I think this way will allow better code maintenance.

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

Sidebar

Related Questions

Im using timestamps with my sql database. I currently am making a voting system,
Making a new class in VS10 gives me using System; using System.Collections.Generic; using System.Linq;
I am making a voting system. When user click on a link with class
I was having trouble with a voting system that I'm trying to make in
I am making a website with a voting system, for some background info, this
Making a painting app using HTML5 and Canvas. I think I want to have
Making a new site but something is happening to it in IE8. The social
Making a new site but something is happening to it in IE. I've purchased
Making a drop down list for my JSP and the values I'm entering won't
Hay i need to hand implemeneting a voting system into a model. I've had

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.