Search code examples
htmlcssinternet-explorer-6yuibgiframe

Using BGIFRAME in IE6 with YUI Autocomplete


I am writing a simple HTML code which uses YUI autocomplete (to display suggestion as you type like Google). But a <select> block is getting displayed over the suggestions list in IE6 only.

It is working fine in other browsers.

I used bgiframe to avoid it because of z-index bug in IE6 but had no luck.

My simple code is here:

<script type="text/javascript" src="http://yui.yahooapis.com/2.8.2r1/build/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.8.2r1/build/connection/connection-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.8.2r1/build/animation/animation-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.8.2r1/build/datasource/datasource-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.8.2r1/build/autocomplete/autocomplete-min.js"></script>

<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript" src="js/jquery.bgiframe.js"></script>

<script type="text/javascript" charset="utf-8">
    $(function() {
        $('#myContainer').bgiframe();
    });
</script>

<style type="text/css">
    #myAutoComplete {
        width:25em; /* set width here or else widget will expand to fit its container */
        padding-bottom:2em;
    }
</style>
</head>
<body>
    <h1>Autocomplete using YUI !</h1>
    <label for="myInput">Search names in our database:</label>
    <div id="myAutoComplete" class="yui-skin-sam">
        <input id="myInput" type="text">
        <div id="myContainer"></div>
    </div>
    <br>
    <div>   
        <form action="#" method="get" accept-charset="utf-8">
            <select>
                <option value="val1">val1</option>
                <option value="val2">val2</option>
            </select>
        </form>
    </div>
</body>

Here select is displayed over myContainer (myContainer displays populated suggestions). I know I am making some blunder. Please help me to figure it out.


Solution

  • jQuery and YUI live in separate namespaces so there shouldn't theoretically be a problem. Are you sure there are no JavaScript errors? Are all the libraries loaded correctly?

    Could use jQuery autocomplete instead?

    Edit: You can configure the YUI autocomplete to use an iFrame! It kind of works in that it does hide the <select> but not instantly. This is probably the best solution since it does not needs jQuery or bgiframe.

    Edit 2: I was not happy with the speed at which the <iframe> was created by YUI so came up with this hack! Here is a complete solution that seems to work in IE6 for me. THe problem is that YUI is in control of the #myContainer which seems to break the bgiframe that jQuery sets up. So I opted to simply manipulate the height of #myContainer with the YUI method hooks. You may need to change this value to fit your layout, but I'm hoping it will work for you.

    Sorry the CSS alteration is jQuery. I have never used YUI before and haven't got any idea how to change CSS properties in YUI :-)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Title</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <style type="text/css">
            #myAutoComplete {
                width:15em; /* set width here or else widget will expand to fit its container */
                padding-bottom:2em;
            }
        </style>
        <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.8.2r1/build/autocomplete/assets/skins/sam/autocomplete.css" />
        <script type="text/javascript" src="http://yui.yahooapis.com/2.8.2r1/build/yahoo-dom-event/yahoo-dom-event.js"></script>
        <script type="text/javascript" src="http://yui.yahooapis.com/2.8.2r1/build/connection/connection-min.js"></script>
        <script type="text/javascript" src="http://yui.yahooapis.com/2.8.2r1/build/animation/animation-min.js"></script>
        <script type="text/javascript" src="http://yui.yahooapis.com/2.8.2r1/build/datasource/datasource-min.js"></script>
        <script type="text/javascript" src="http://yui.yahooapis.com/2.8.2r1/build/autocomplete/autocomplete-min.js"></script>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
        <script type="text/javascript" src="jquery.bgiframe.min.js"></script>
    </head>
    <body class="yui-skin-sam">
        <div id="myAutoComplete">
            <label for="myInput">Enter a state:</label><br/>
            <input id="myInput" type="text"/>
            <div id="myContainer"></div>
        </div>
        <div>
            <form action="#" method="get" accept-charset="utf-8">
                <p>
                    <select>
                        <option value="val1">val1</option>
                        <option value="val2">val2</option>
                    </select>
                </p>
            </form>
        </div>
        <script type="text/javascript">
            $(function() {
                $('#myContainer').bgiframe();
            });
    
            YAHOO.example.Data = {
                arrayStates: [
                    'Alabama',
                    'Alaska',
                    'Arizona',
                    'Arkansas',
                    'New Hampshire',
                    'New Jersey',
                    'New Mexico',
                    'New York',
                    'Wyoming'
                ]
            }
    
            YAHOO.example.BasicLocal = function() {
                var oDS = new YAHOO.util.LocalDataSource(YAHOO.example.Data.arrayStates);
                var restoreHeight = function(sType, aArgs) {
                    $('#myContainer').css({height:'auto'});
                };
    
                // Instantiate the AutoComplete
                var oAC = new YAHOO.widget.AutoComplete("myInput", "myContainer", oDS);
                oAC.prehighlightClassName = "yui-ac-prehighlight";
             //   oAC.useIFrame = true; // works but changes the container into an iFrame a bit too late for my liking
                oAC.doBeforeExpandContainer = function () {
                    $('#myContainer').css({height:'50px'}); // might need to play around with this value
                    return true;
                }
                // restore height
                oAC.containerCollapseEvent.subscribe(restoreHeight);
                oAC.useShadow = true;
    
                return {
                    oDS: oDS,
                    oAC: oAC
                };
            }();
        </script>
    </body>
    </html>