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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:44:26+00:00 2026-06-09T22:44:26+00:00

Using JS/jQuery, what is the best way to do this? I have 5 variables:

  • 0

Using JS/jQuery, what is the best way to do this?

I have 5 variables:

var fruit1 = "apple";
var fruit2 = "mango";
var fruit3 = "orange";
var fruit4 = "banana";
var fruit5 = "melon";

Then I have a list element with a certain fruit which is clicked:

<li id="banana">Banana</li>

I get its ID in the script:

$("li").on("click", function() {
    var selectedfruit = $(this).attr("id");
});

How do I match selectedfruit with the variable list so that it returns fruit4 with which I can do stuff with?

Secondary question, should I put my variable list in an array?

Thanks very much.

Edit: I am very sorry, but I have made a huge mistake

I need to validate the selectedfruit with the variable name, not the variable contents.

So, the markup would be like this:

<li id="fruit4">Mystery fruit</li>

  • 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-09T22:44:27+00:00Added an answer on June 9, 2026 at 10:44 pm

    Yes, this is definitely a job for an array.

    var fruits = ["apple", "mango", "orange", "banana", "melon"];
    

    Then in your click handler, you can search this array, and get its index.

    $("li").on("click", function() {
        // Note: This will return -1, if it's not in the array
        var selectedfruit = $.inArray($(this).attr("id"), fruits); // 3 (arrays are zero-indexed)
    });
    

    UPDATE Based off of your update, I would use an object instead.

    var fruits = {
        fruit1: "apple",
        fruit2: "mango",
        fruit3: "orange",
        fruit4: "banana",
        fruit5: "melon"
    };
    

    Then you can use the ID to get the value and compare.

    $("li").on("click", function() {
        var selectedfruit = fruits[$(this).attr("id")];  // banana
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first day coding and using jquery, I figured the best way
I'm trying to figure out the best way to do this...I am using JQuery
Without using jQuery, what is the best way to limit text entry of a
What's the best way for selecting a table row by index using jQuery? Thanks
What is the best possible way using jQuery to give height and width to
I'm not using JQuery but I know they have functions to do this. I
I'm using the jQuery validate plugin. I have this code: $(.btn-overview-basic-save).click(function(e) { if (!$('.contact-overview').valid())
Using jquery, what is the best way to delete all divs with a certain
What is the best jquery twitter plugin capable of using the twitter lists API?
Using Jquery , I have an array result [<a href=><img src=image1></a>,<a href=><img src=image2></a>] 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.