Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$(this).hide() not working on option elements in IE8

I have a problem in my jquery code. I want to make a cascading dropdown using jquery. Below is my code for it.

HTML

<SELECT class="input_style" name="comp_dd" id="comp_dd">
    <option value="0">[Select]</option>
    <OPTION value="1">Company1</OPTION> 
    <OPTION value="2">Company2</OPTION> 
    <OPTION value="3">Company3</OPTION> 
    <OPTION value="4">Company4</OPTION> 
</SELECT>

<SELECT class="input_style" name="group_dd" id="group_dd">
    <option data-parent="-1" value="0">[Select]</option>
    <OPTION  data-parent="1,3"; value="1" >grp_f</OPTION> 
    <OPTION  data-parent="1,2"; value="2" >grp_e</OPTION> 
    <OPTION  data-parent="1,3,4"; value="3" >grp_t</OPTION> 
</SELECT>

jquery code

$().ready(function() {  

    $('#comp_dd').change(function() {
       var parent = $(this).val();
      if(parent!=0)
      {
       $('#group_dd').children().each(function() {
         var listOfNumbers = $(this).data('parent');        
        if($(this).data('parent')!='-1')
        {   

             var numbers = listOfNumbers.split(',');            
             if(jQuery.inArray(parent, numbers)!=-1 )
             {               
                  $(this).show();
            }
            else
            {               

                 $(this).hide();

            }
        }       
       });
      }
      else
      {
        $('#group_dd').children().each(function() {
             $(this).show();
       });
      }
    });
});

code works correctly into chrome and FF but not working in IE7 & IE8. .hide() is not working in IE7 and IE8

Please help me to get rid of it.

Thanks in advance

ANSWER:(given by Paulo Rodrigues)

js code:

var original = $('#group_dd').clone();

$('#comp_dd').change(function() {
    var parent = $(this).val();

    $('#group_dd').empty().append($(original).html());

    if (parent != 0) {
        $('#group_dd').children().each(function() {
            var listOfNumbers = $(this).data('parent');        
            if ($(this).data('parent')!='-1') {
                var numbers = listOfNumbers.split(',');

                if (jQuery.inArray(parent, numbers)==-1 ) {
                    $(this).remove();
                }
            }
       });
    }
});
like image 231
pkachhia Avatar asked Feb 28 '12 12:02

pkachhia


2 Answers

.hide() will change style display to none, and IE not allow this. So I recommend you remove this element.

like image 102
Paulo Rodrigues Avatar answered Oct 02 '22 00:10

Paulo Rodrigues


Use .detach() in jquery.

DEMO

$('#comp_dd').change(function() {
    var parent = $(this).val();
    if (parent != 0) {
        $('#group_dd').children().each(function() {
            var listOfNumbers = $(this).data('parent');
            if ($(this).data('parent') != '-1') {
                var numbers = listOfNumbers.split(',');
                if (jQuery.inArray(parent, numbers) != -1) {
                    $(this).show();
                }
                else {
                    alert($(this).val()+" detached");
                    $(this).detach();
                }
            }
        });
    }
    else {
        $('#group_dd').children().each(function() {
            $(this).show();
        });
    }
});​
like image 36
footy Avatar answered Oct 02 '22 00:10

footy