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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:30:55+00:00 2026-06-03T04:30:55+00:00

I am using this script to update the database on select change in a

  • 0

I am using this script to update the database on select change in a form :

$(document).ready(function() {
        $('select.changeStatus').change(function(){
                $.ajax({
                        type: 'POST',
                        url: 'Change-status.php',
                        data: {selectFieldValue: $('select.changeStatus').val(), projectId: $('input[name$="projectId"]').val()},
                        success: function(data){},
                        dataType: 'html'
                 });
        });
});

I’d like to insert a switch statement in the success setting in order to modify the CSS class of the <div/> containing my form according to certain conditions. The idea is to do this :

If selectFieldValue is 0, make my <div/> background red, if it’s 1, make the background orange, if it’s 2, make the background green.

I’m actually planning on using this : $("#mydiv").css({'background-color': "red"}), I’m just having trouble nesting it in a switch within the success setting.

How can I do so? Or, is there a better way to do this?

Thanks a lot.

  • 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-03T04:30:56+00:00Added an answer on June 3, 2026 at 4:30 am

    A switch statement would be a bit verbose. If the value will be 0, 1, or 2, you can use those as indexes to lookup a value from within an array of values:

    success: function(data){
      var colors = [ "red", "orange", "green" ],
          _value = $('select.changeStatus').val();
      $("#mydiv").css("background", colors[ _value ]);
    }
    

    Update:

    OP asks: Excellent, thanks a lot. In the case of ranges (0-1 for red, 2-3 for orange, 4-5 for green), is there a better way to do this than the following?

    var colors = [ "red", "red", "orange", "orange", "green", "green" ]

    If you’re wanting to form collections of numbers, where 0 and 1 result in one color, 2 and 3 result in another color, and 4 to 5 result in yet another, I would approach this slightly differently. We know that if the number is odd, it is on the high-end of the collection. After all, 2-3, and 4-5, both have odd numbers on their high-end. This means, if the number is odd, it should behave the same as its earlier contiguous partner. So if the number was five or four, we use four. If it was three or two, we use two.

    success: function(data){
      var colors = [ "red", "orange", "green" ],
          _value = $('select.changeStatus').val();
    
      if ( _value % 2 != 0 ) _value--; // Converts 1 to 0, 3 to 2, 5 to 4, etc.
    
      $("#mydiv").css("background", colors[ _value ]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hay, i'm using this script function preloader(images){ var i = 0; imageObj = new
Recently I've been looking at opening a tab using this script: $('.tofour').click(function() { //
I am using this Ajax script below to make cross domain Ajax calls which
I'm using this script to display all the images in a folder, but I
I'm using this script which works fine for a single lightbox popup window. I
I have a file hosting site that's using this script MFHS . I want
i have only ftp credentials in which i am using this script $r=mysqldump $dbuser
in my site i want to set default page to open, using this script
I want to display an image using this php script on my server. $file
Why does this script return a pair of nulls? I'm using SQL Server 2008,

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.