tags:

views:

32

answers:

1

The following example works fine in Firefox and Opera, but not in ie8. However, if I turn compatibility mode on in ie8, it works correctly. Why? And how do I fix it?

The below example shows the use of toggleClass, but replacing this with addClass and removeClass still has the same problem.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript" src="/jquery/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="/jquery/js/jquery-ui-1.7.2.custom.min.js"></script>
<style type="text/css">
    .dostuff{
        background-color: green;
    }
    .undostuff{
        background-color: red;
    }
</style>
<script language="javascript">

    $(document).ready(function(){
        $('#DoStuffButton').click(function(){doStuff();});
        $('#UndoStuffButton').click(function(){undoStuff();});

        function doStuff(){
            $('#test').html('Do some stuff');
            $('#test').toggleClass('dostuff', 'slow', callback('dostuff'));
        }

        function undoStuff(){
            $('#test').html('Undo some stuff');
            $('#test').toggleClass('undostuff', 'slow', callback('undostuff'));
        }

        function callback(className){
            setTimeout(function(){
                $('#test').toggleClass(className, 'slow');
            }, 1500);
        }


    });
</script>
</head>
<body>
    <div id="test">Blah blah blah</div>
    <div>
        <button id="DoStuffButton" name="DoStuffButton">Do some stuff</button>
        <button id="UndoStuffButton" name="UndoStuffButton">Undo some stuff</button>
    </div>
</body>
</html>

EDIT: Changing the javascript to the following also has the same problem in ie8. Any explanation why, and how to get it to work correctly?

function doStuff(){
            $('#test').html('Do some stuff');
            $('#test').addClass('dostuff', 'fast');
            setTimeout(function(){$('#test').removeClass('dostuff', 'slow');}, 1000);
        }

        function undoStuff(){
            $('#test').html('Undo some stuff');
            $('#test').addClass('undostuff', 100);
            setTimeout(function(){$('#test').removeClass('undostuff', 'slow');}, 1000);
        }
+2  A: 

It may be doing what you want it to on some browsers, but it's not doing it the way you think it is.

Firstly, your parameters to toggleClass don't match the API. so 'slow', callback('dostuff') mean nothing to toggleClass.

Secondly, when you pass callback('dostuff') you're passing the result of calling that function, you're not passing a reference to the function.

Sam Hasler
if I change to to addClass and removeClass to match the API, even if I pass an anonymous function to the addClass, I get the same result. Will passing an anonymous function to addClass still be passing the result of calling that function?
Jayson
@Jayson, neither addClass, removeClass or toggleClass accept a callback function as a parameter. It only works in your example because `callback('dostuff')` is called **straight away** when it is passed and you're actually passing in undefined as the callback function doesn't return anything.
Sam Hasler
you right, the API doesn't specify a callback. My mistake was I was following the example at http://jqueryui.com/demos/addClass/. However, even if I do the following, it still doesn't work in IE8. Any explanation for this?function doStuff(){ $('#test').html('Do some stuff'); $('#test').addClass('dostuff', 'fast'); setTimeout(function(){$('#test').removeClass('dostuff', 'slow');}, 1000); }
Jayson
@Jayson 'fast' and 'slow' are invalid parameters. And it works for me in IE8: http://jsbin.com/evivu
Sam Hasler
invalid parameters are in the example jqueryui.com/demos/addClass. Removing the parameters solves the problem.
Jayson