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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:46:20+00:00 2026-06-08T10:46:20+00:00

I’ve got a Javascript array, like this : var fruits=[‘apple’,’orange’,’peach’,’strawberry’,’mango’] And I want to

  • 0

I’ve got a Javascript array, like this :

var fruits=['apple','orange','peach','strawberry','mango']

And I want to add an event on these elements who will give me varieties on my database. For example ‘apple’ will give me

<div class="apple">golden</div>
<div class="apple">granny</div>
<div class="apple">canada</div>

Moreover I’ve got nothing on my database about mango varieties, I want to highlight ‘mango’ from the rest of the array. I’ve already created my classes like this:

.black {
    color: black;
    pointer-events: none;
    cursor: default;
}

.couleur {
    color: blue;
}

Have I to add HTML code?
Thanks for your help, it’s only my second post on stackoverflow so sorry for the weird code samples.

  • 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-08T10:46:21+00:00Added an answer on June 8, 2026 at 10:46 am

    OK, since this is your second post, I’m going to assume that you’re relatively new to web development. Welcome!

    Your question isn’t very clear on what you want to happen, so I’ll assume simplicity first: we’ll create an onclick event that does an AJAX call to a php script (you tagged php, so I’ll assume you’re writing php scripts) and then displays the results in a dynamic div on the page.

    I’m going to use some alternative styles of php scripting, so don’t be scared when you see

    if ($something):
        // do something
    else:
        // do something else
    endif;
    

    For starters, let’s set up our elements:

    <?php
    // Assume we already have the fruit categories stored in an array, $categories
    // Display each fruit category on the page
    foreach ($categories as $category) :
    ?>
        <div class='category' id='<?=$category?>'><?=$category?></div>
    
    <?php
    endforeach;
    ?>
    <!-- empty placeholder div - this is where our results will go -->
    <div id='results'></div>
    

    This will display all of the elements we want on the page to start. If you want to style these elements based on the category class, go right ahead.

    Next we need to write some javascript that will determine the onClick behavior. For this we can use straight javascript, or we can use jQuery (which is VERY prevalent today – read up on it, it’s not that difficult to grasp).

    <script type='text/javascript' language='javascript'>
        $('.category').click(function() {
            // grab the category (based on the id of the element) and send it to our background page
            var category = $(this).id();
            $.post('backgroundScript.php', 
                {
                    type: category
                }, 
                function(data) {
                    // print the results inside the #results div
                    $('#results').html(data);
                }
            );
        });        
    </script>
    

    Finally, we need a background script to process our data and return our results.

    <?php
    //backgroundScript.php
    
    $type = $_POST['type'];
    
    // pass $type in to the database and perform some joins to get the subcategories in an array,
    // then present a list of subcategories
    $subcategories = findSubCategoriesInDB($type);
    
    if (count($subcategories)) {
        echo "<ul>";
        foreach ($subcategories as $subcat) {
            echo "<li>" . $subcat . "</li>";
        }
        echo "</ul>";
    } else {
        echo "No subcategories were found for " . $type;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I used javascript for loading a picture on my website depending on which small

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.