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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex