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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:35:54+00:00 2026-05-16T17:35:54+00:00

This seems like overkill and I would like to refactor this…any suggestions if($(this).text() ==

  • 0

This seems like overkill and I would like to refactor this…any suggestions

    if($(this).text() == "Grocery"){
        $(".type_changer").attr("id", "gro");
    }else if($(this).text() == "Restaurant"){
        $(".type_changer").attr("id", "res");
    }else if($(this).text() == "Bar"){
        $(".type_changer").attr("id", "bar");
    }else if($(this).text() == "Pizza Delivery"){
        $(".type_changer").attr("id", "piz");
    }else if($(this).text() == "Quick Service"){
        $(".type_changer").attr("id", "qui");
    }else if($(this).text() == "Retail"){
        $(".type_changer").attr("id", "ret");
    }else if($(this).text() == "Salon"){
        $(".type_changer").attr("id", "sal");
    }
    if($(this).attr("id").slice(-1) == 1){
        $(".number_changer").attr("id", "one1");
    }else if($(this).attr("id").slice(-1) == 2){
        $(".number_changer").attr("id", "two2");
    }else if($(this).attr("id").slice(-1) == 3){
        $(".number_changer").attr("id", "three3");
    }else if($(this).attr("id").slice(-1) == 4){
        $(".number_changer").attr("id", "four4");
    }else if($(this).attr("id").slice(-1) == 5){
        $(".number_changer").attr("id", "five5");}
  • 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-16T17:35:55+00:00Added an answer on May 16, 2026 at 5:35 pm

    Look here,

    if($(this).text() == "Grocery"){
        $(".type_changer").attr("id", "gro");
    }else if($(this).text() == "Restaurant"){
        $(".type_changer").attr("id", "res");
    }else if($(this).text() == "Bar"){
        $(".type_changer").attr("id", "bar");
    }else if($(this).text() == "Pizza Delivery"){
        $(".type_changer").attr("id", "piz");
    }else if($(this).text() == "Quick Service"){
        $(".type_changer").attr("id", "qui");
    }else if($(this).text() == "Retail"){
        $(".type_changer").attr("id", "ret");
    }else if($(this).text() == "Salon"){
        $(".type_changer").attr("id", "sal");
    }
    

    You have to think all the repetitions away. What would left over? Right, the text and id values:

    "Grocery", "gro"
    "Restaurant", "res"
    "Bar", "bar"
    "Pizza Delivery", "piz"
    "Quick Service", "qui"
    "Retail", "ret"
    "Salon", "sal"
    

    Let hold them in some data structure. An object is an obvious choice.

    var types = {
        "Grocery": "gro",
        "Restaurant": "res",
        "Bar": "bar",
        "Pizza Delivery": "piz",
        "Quick Service": "qui",
        "Retail": "ret",
        "Salon": "sal"
    }
    

    It can be accessed like an associative array with dynamic keys. Now you can use a single line:

    $(".type_changer").attr("id", types[$(this).text()]);
    

    How to replace the second part is left as exercise to you, but it boils down to the same.


    Update: you seem to have a hard time in understanding this. Here’s an explanation from my side:

    When $(this).text() returns "Grocery", the above will resolve to

    $(".type_changer").attr("id", types["Grocery"]);
    

    The types["Grocery"] will in turn return "gro", so it basically ends up as

    $(".type_changer").attr("id", "gro");
    

    when $(this).text() is "Grocery".

    See also:

    • JavaScript Object Notation (JSON) tutorial
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like a silly question but I would really like your comments and
This seems like a pretty softball question, but I always have a hard time
This seems like a simple question, but I can't find it with the Stack
This seems like a very simple and a very common problem. The simplest example
This seems like such a trivial problem, but I can't seem to pin how
This seems like perhaps a naive question, but I got into a discussion with
How can I construct my ajaxSend call, this seems like the place to put
Can someone tell me how to change directories using FtpWebRequest? This seems like it
This sure seems like a simple error to fix. However, I somehow can't figure
It seems like this should be straightforward but I'm boggling. I've got my listview

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.