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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:10:48+00:00 2026-06-18T16:10:48+00:00

i am quite new to Jquery and Javascript but i ve created a script

  • 0

i am quite new to Jquery and Javascript but i ve created a script which communicates with a php controller in symfony2.

My question is, what can i do to reduce the script length –> maybe there is a smarter way to solve the logic which i solved with the switch expression?

Thanks

/**
 * This function gets the current clicked buttons value and post it to the given action + returns the correct value
 */

$(document).ready(function() {
    $('button.rating-button').click(function(event) {
        event.preventDefault();
        var $element   = $(this).attr('id');
        var $rating    = $(this).attr('value');
        var $messageId = $(this).closest('ul').next('input:hidden').val();

        if(typeof $rating !== 'undefined' && $rating != null) {
            $.post(Routing.generate('rating_vote', { 'messageId': $messageId, 'ratingCode': $rating }),
            function($json) {
                $('button#'+$element).parent().find('span').text($json.numberOfRatings);

                /* check if oldRating is defined and oldRating is not the same as new rating ( prevent decrease of actual value if this is the first vote entry for the current message and the specific user ) */
                if(typeof $json.oldRating !== 'undefined' && $json.oldRating != null && ($json.oldRating != $rating) ) {
                    switch($json.oldRating) {
                        case 1:
                            $oldElement = 'rating-first';
                            break
                        case 2:
                            $oldElement = 'rating-second';
                            break;
                        case 3:
                            $oldElement = 'rating-third';
                            break;
                    }
                    $('button#'+$oldElement).parent().find('span').text(Number($('button#'+$oldElement).parent().find('span').text()) -1);
                }

            });
        }

    });
});
  • 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-06-18T16:10:50+00:00Added an answer on June 18, 2026 at 4:10 pm

    you can replace your switch case logic with this rather neat concept.

    $(function() {
        $('button.rating-button').on('click',function(event) {
            event.preventDefault();
            var $this = $(this),
                element   = $this.attr('id'),
                rating    = $this.attr('value'),
                messageId = $this.closest('ul').next('input:hidden').val();
            if(typeof rating !== 'undefined' && rating != null) {
                $.post(Routing.generate('rating_vote', { 'messageId': messageId, 'ratingCode': rating }),
                    function(json) {
                        $('button#'+element).parent().find('span').text(json.numberOfRatings);
                        if(typeof json.oldRating !== 'undefined' && json.oldRating != null && (json.oldRating != rating) ) {
                            var oldElement = ({
                                1: 'rating-first',
                                2: 'rating-second',
                                3: 'rating-third'
                            })[json.oldRating];
                            $('button#'+oldElement).parent().find('span').text(Number($('button#'+oldElement).parent().find('span').text()) -1);
                        }
                    }
                );
            }
        });
    });
    

    Some general thoughts:

    • when using jQuery, use $[varname] as an indicator of a variable containing a jQuery object (knowing your php background this can feel very strange in the beginning)
    • creating a jQuery-Object is kind of heavy on the performance side so make sure you create them only once preferably
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question is quite basic but I am new to JQuery and
I'm pretty new with Javascript and jQuery and can't quite figure out settimeout .
Ok, so I'm quite new to jQuery, but found this bizzare problem just now,
I'm quite new to the whole jQuery/JSON thing but thought I would give it
I am very new to javascript and have been using the JQuery library quite
I'm quite new to javascript and JQuery programming. Usually, to access elements I give
I'm very familiar with HTML and CSS, but new to Javascript and jQuery. I
I'm quite new to jQuery and can't find out what doesn't work. Tn the
I'm quite new to Javascript. I've been trying to figure this one out but
Im new to Jquery but it turns out I used it quite a bit

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.