views:

502

answers:

2

I'm using jQuery to switch between 'simple' and 'advanced' search panels by sliding up/down the panels. After the slide I want to set the input focus to the first field, however this causes the panel to blank out on IE7. It doesn't happen on FF 3.5.1

The original code is using ASP.NET but I've managed to reproduce it with some simple HTML and JavaScript below. My first attempt was based on tables rather than divs, but the same problem occurred.

I thought it might be the peekaboo bug but I tried some of the suggested fixes (position:relative, zoom:1) but no luck.

When the demo page is opened the simple search div is displayed, when you click on the 'Advanced' button then the advanced panel slides down but is then hidden. If you resize the window then it is shown again. Removing the jquery() call to focus() fixes the problem.

<html>
<head>
<title>Focus Demo</title>
<script src="jquery.js" type="text/javascript"></script>

<style type="text/css">

      .criteriaRow
    {
        clear:both;
    } 

     .criteriaLabel
    {
        float:left;
        width:200px;
    }              

</style>

<script type="text/javascript">
    $(document).ready(function() {

        $("#btnSwitchAdvanced").click(function() {
            $("#simpleSearchPanel").slideUp(200, function() {
                $("#advSearchPanel").slideDown(500, function() {
                $("#txtASDescription").focus();
                });
            });
        });

        $("#btnSwitchSimple").click(function() {
            $("#advSearchPanel").slideUp(200, function() {
                $("#simpleSearchPanel").slideDown(500, function() {
                   $("#txtSimpleSearch").focus();
                });
            });
        });

        $("#advSearchPanel").hide();
    });
</script>

</head>
<body>
<div id="searchPanel" style="background-color: #dcdcdc; margin-top: 5px;">
    <div id="searchCriteria">
        <div id="simpleSearchPanel">
            <span>Simple Search</span>
            <input type="text" id="txtSimpleSearch" />
            <input type="button" id="btnSwitchAdvanced" value="Switch to Advanced" />
        </div>
        <div id="advSearchPanel">
            <span>Advanded Search</span>
            <div>
                <div class="criteriaRow">
                    <div class="criteriaLabel">
                        <span id="lblASDescription">Description:</span>
                    </div>
                    <div class="criteriaInput">
                        <input  type="text" id="txtASDescription" />
                    </div>
                </div>

                <div class="criteriaRow">
                    <div class="criteriaLabel">
                        <span id="lblASId">Id:</span>
                    </div>
                    <div class="criteriaInput">
                        <input name="txtASId" type="text" id="txtASId" />
                    </div>
                </div>
            </div>

            <div>
                <div id="criteriaActions" style="float:left;">
                    <input type="button" id="btnSwitchSimple" value="Switch To Simple" />
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>
A: 

I have it working in ie8 after switching this line:

 $("#advSearchPanel").slideDown(500, function() {

to this line:

 $("#advSearchPanel").show(function() {
Matrym
PS: Although I'm relatively a noob here, you may consider using the full patha to google's CDN jquery, so people can copy and paste your code to use immediately:<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js?ver=1.3.2'></script>
Matrym
I'm putting a -1 on this answer because it does not solve the problem. It changes the SLIDE functionality to SHOW / HIDE. The guy is clearly asking to make SLIDE work properly.
Josh
@Josh: Remind me not to add information to a question next time.
Matrym
@Matrym, to be clear - his issue was with IE7 and Slide functionality. Maybe doing a "-1" is a big too "strong" of an action to do. I'm not sure how else to point out that this is not a solution to his problem. No offense intended - this community adds controls like +/- as a means of being nice and helping solve the problem. :) There should be a "not a solution" button instead.
Josh
@Josh I wish comments could have code lines in them :/. No offense taken then.
Matrym
A: 

@canice, I've run into the same / similar situation before, where NOTHING seems to resolve the issue in IE7 or IE6. I've done all of the hacks - zoom, position, z-index, etc., and ultimately I ended up adding an extra bit of code for those blasted browsers.

Most cases I can resolve by using jQuery to dynamically change the dimensions of the element that is playing peekaboo by adding or subtracting 1 pixel after the animation function runs.

I admit that this is not by any means "perfect," but it does the job and sometimes that's all we have the time or resources for.

You can find more info about browser-specific checking using jQuery here: http://api.jquery.com/jQuery.browser/

Alternatively, using code inside blocks such as the following might be useful too:

<!--[if lte IE 6]>
<script type="text/javascript">
$(document).ready(function() {
    $(element).dostuff();
});
</script>
<![endif]-->
Josh