Skip to main content

Hide select option in IE using jQuery



Currently I am using jQuery to hide/show select options using following code.







$("#custcol7 option[value=" + sizeValue + "]").hide();







This works fine in Firefox, but doesnt do any good in other browsers. How to I hide options from select in Chrome, Opera and IE?



Source: Tips4all

Comments

  1. I just came across this and instead of cloning the entire select over and over I just replaced the options that need to be hidden with span elements and hiding the spans ( though the browser didnt visually show them anyway, I think ) - you may need to change your code ( if complex ) to iterate through the spans for complex logic.

    The spans store a reference to the option and replace themselves with it when they need to be shown.

    This code can obviously be refactored and prettified.

    http://fiddle.jshell.net/FAkEK/12/show/

    EDIT #2 ( USE THIS INSTEAD ): It occurred to me that instead of doing all this clone/reference/replace crap, just wrap the option with a span, hide the span, and on show just replace the span with the option again..

    http://fiddle.jshell.net/FAkEK/25/show/

    ReplyDelete
  2. try detach().
    you can reattach it later if needed using append() or insertAfter()

    ReplyDelete
  3. You don't, it's not supported in IE (and assumably not in Chrome or Opera either). You would have to remove the options altogether and add them back later if you want them to be truly invisible. But in most cases a simple disabled="disabled" should suffice and is a heck of a lot simpler than handling the removing and adding of options.

    ReplyDelete
  4. Just deleted it and store it in a var in your JavaScript. You can just create the new object when you need it later.

    Otherwise try the disabled attribute mentioned above.

    ReplyDelete
  5. There's also the the .load method:

    s_parent.change(function(){
    s_child.load('./fetch_options.php",{'v',s_parent.val()});
    }


    The 'fetch_options.php' script would simply print the option tags based on whatever data source you use and the parent value(s) being passed in.

    ReplyDelete
  6. hope it's coo to post on old threads... here is my solution

    //Where #dateOptionsTX and #dateOptions are hidden <select> elements in the html
    if ($("#state").val()=="TX" && DateIsEligible=='yes')
    {
    $("#start_date").html($("#dateOptionsTX").html());
    }
    else
    {
    $("#start_date").html($("#dateOptions").html());
    }
    //PS all this work just to show a few extra options for IE..LOL

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.