0

I have following array ,which consists of category and subcategory.

        $event_category = array(
        'Entertainment' => array('Music/Concerts', 'Theatre and Arts', 'Dance and Yoga', 'Night life and Parties', 'Food and Dining', 'Festivals', 'Kids Events', 'Awards', 'Others'),
        'Sports' => array('Adventure', 'Cricket', 'Cycling', 'Fitness', 'Marathon', 'Others'),
        'Exhibhitions Fairs' => array('Technology', 'Education', 'Real Estate', 'Travel, Tourism, leisure', 'Trade Fairs', 'Others'),
        'College Events' => array('College Fest', 'Alumni Meet', 'Summer Camps', 'Others'),
        'Corporate' => array('Conferences', 'Seminars', 'Others'),
        'Competitions' => array(),
        'Awards' => array(),
        'Not for profit' => array(),
        'Other' => array(),
    );

Now I am populating the above array as category and subcategory.

  <select style="color:#999;" name="category" id="category">
          <option  value="0"> Select </option>
         <?php foreach ($event_category as $key => $val) { ?>
           <option value="<?php echo $key; ?>" style="color:#585858;"><?php echo $key; ?></option>  
        <?php }
        ?>
   </select>


    <select  style="color:#999;" name="subcategory" id="subcategory">
   <option  style="color:#999;" class="axyzb" value="0"> Select </option>
      <?php
        foreach ($event_category as $key => $val) {
        foreach ($val as $value) { ?>
      <option style="display: none;color:#999;" class="<?php echo str_replace(' ', '_', $key); ?>" value="<?php echo $value ?>"><?php echo $value ?></option> 
      <?php
       }
       }
    ?>
   </select>

Initially all the subcategories are display:none except the select one . all the subcategory has class which is its main category. Now when I change the category I hide all the subcategories, take the value of selected category and show() the subcategories whose class is the value which I got from category like this

     $('#category').change(function () {
                $("#subcategory").children('option').hide();
                var cate = document.getElementById('category').value;
                cate = cate.split(' ').join('_');
                if (cate != '') {
                    $('.' + cate).show();
                    $('.axyzb').show();
                }

            })

basically I want to show subcategories according to the selected category..above code works fine for me in firefox but in chrome it does not work for two categories .when I check the console the initial display:none is removed from the html but still those subcategories are not shown on the page.

1 Answers1

1

You can't show and hide <option> elements, it's not supported in all browsers, you have to actually remove them and reinsert them.

Chrome and IE have issues with hiding options, and it's strange that it would work intermittently, as it probably shouldn't work at all.

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • Right i have been in such issues with chrome and firefox once. – Jai Jan 06 '15 at 09:58
  • @adeneo can you please tell me how can I remove and reinsert the options ? – user3230879 Jan 06 '15 at 10:02
  • @user3230879 - that would be a lot more complicated, as you'd have to keep the ones you remove somewhere, so they can be inserted later etc. – adeneo Jan 06 '15 at 10:05
  • @user3230879 - if you want someone to work it out for you, you should probably post the HTML, I can't really duplicate your serverside code here. – adeneo Jan 06 '15 at 10:07
  • @adeneo can you please give me a idea how should I do it. – user3230879 Jan 06 '15 at 10:10
  • I would probably use jQuery's `data()` to hold an object with the removed options, use `detach()` to remove them, and then just reinsert them etc. I know there's an answer on here somewhere with a small plugin for filtering options, try a search. – adeneo Jan 06 '15 at 10:15
  • @user3230879 [How to dynamic filter options of – Andreas Jan 06 '15 at 10:48