views:

1192

answers:

4

Is there any reason why the click doesn't work in IE6 on the following JQuery code? See ...$('#toggleVAT').click(function... It works in IE7 and FF?

function switchButton(to){
    if(to === 'INC'){
 $('#toggleVAT').removeClass("exc");
 $('#toggleVAT').addClass("inc");
 $('#toggleVAT em').text("inc.");
    } else {
 $('#toggleVAT').addClass("exc");
 $('#toggleVAT').removeClass("inc");
 $('#toggleVAT em').text("exc.");
    }
}

function switchPrices(){
    if($.cookie('VATMODE') == "INC"){
 $('.price .incVAT').show();
 $('.price .excVAT').hide();
 switchButton('INC');
    } else {
 $('.price .incVAT').hide();
 $('.price .excVAT').show();
 switchButton('EX');
    }
}


$(function(){
    switchPrices();
    $('#toggleVAT').click(function(){
 if($.cookie('VATMODE') === 'INC'){
     $.cookie('VATMODE', 'EX');
     switchButton('EX');
 } else {
     $.cookie('VATMODE', 'INC');
     switchButton('INC');
 }
 switchPrices();
 return false;
    });
});

On IE6 switchPrices() runs once, but it does not execute the code when I click #toggleVAT. I am using latest minified jQuery. #toggleVAT is just a paragraph. I am using IETester http://my-debugbar.com/wiki/IETester/HomePage. I checked it on natively running IE6 before and the bahaviour was the same. I've also rulled out possible CSS issues as the problem persists without stylesheet.

+1  A: 

Is toggleVAT a link? Try returning false in the callback.

$('#toggleVAT').click(function(){
        if($.cookie('VATMODE') === 'INC'){
            $.cookie('VATMODE', 'EX');
            switchButton('EX');
        } else {
            $.cookie('VATMODE', 'INC');
            switchButton('INC');
        }
        switchPrices();
        return false;
    });

Doing this you prevent the default click behavior being executed.

Daniel Moura
#toggleVAT is not a link, just a paragraph. Return false did not help either. I am using IETester http://www.my-debugbar.com/wiki/IETester/HomePage. It runs JavaScript pretty well. I checked it on natively running IE6 before and the bahaviour was the same.
Frank Malina
+1  A: 

I recall that there are problems when you have multiple (--> hacked) IE versions running on the same windows.

Also if you use the IE Developer Toolbar check that it doesn't disable cookies.


For me your code works without problems in IE6, FF 3.0.11 and Opera 9.64.

I provided a small sample. To test out if you still can reproduce the error.

http://pastebin.com/m7e8d87c6

jquery.js --> http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js

jquery.cookie.js --> http://plugins.jquery.com/files/jquery.cookie.js.txt

jitter
added small sample to test
jitter
+1  A: 

Aren't you calling switchButton code twice? Once inside the click handler and second time inside the switchPrices function? Wouldn't that toggle the values back to the original state?

Try this:

Aren't you calling switchButtonc ode twice? Once inside the click handler and second time inside the switchPrices function?

Try this:

$(
    function()
    {
        $('#toggleVAT').click(
            function()
            {
                switchPrices();
                return false;
            }
           );
        switchPrices();
    }
);

function switchPrices()
{
    var targetVatMode = $.cookie('VATMODE') == 'INC' ? 'EX' : 'INC';
    $.cookie('VATMODE', targetVatMode);
    var removeClassName = targetVatMode == 'INC' ? 'exc' : 'inc';
    var addClassName = targetVatMode == 'INC' ? 'inc' : 'exc';
    var displayText = targetVatMode == 'INC' ? 'inc.' : 'exc.';

    var elementToShow = targetVatMode == 'INC' ? '.price .incVAT' : '.price .excVAT';
    var elementToHide = targetVatMode == 'INC' ? '.price .excVAT' : '.price .incVAT';

    $(elementToShow).show();
    $(elementToHide).hide();


    $('#toggleVAT')
     .addClass(addClassName)
     .removeClass(removeClassName)
     .find('em')
      .text(displayText);
}

Also, if you are okie with Vat Mode as 'EXC' instead of 'EX', you can simplify the code even further.

function switchPrices()
{
    var oldVatMode = $.cookie('VATMODE');
     //Define your Vat mode as EXC instead of EX.
    var newVatMode = oldVatMode == 'INC' ? 'EXC' : 'INC';
    $.cookie('VATMODE', newVatMode);

$('#toggleVAT')
     .addClass(newVatMode.toLowerCase()) 
     .removeClass(oldVatMode.toLowerCase())
     .find('em')
      .text(newVatMode.toLowerCase() + '.');

    $('.price .' + newVatMode + 'VAT').show();
    $('.price .' + oldVatMode + 'VAT').hide();
}
SolutionYogi
Your 1st piece works, but not in IE6. It is quite surreal, I must start thinking out of the box. I've just installed another IE tester (multipleIEs from tredosoft) and the problem persists. I've tried without stylesheets, I've tried stripping all the other JavaScript. I've tried hardcoded onlclick="switchPrices()". No difference.
Frank Malina
Just a wild guess, can you check if you are setting the right DOCTYPE for your page? I am wondering there could be bug due to the IE's Quirks mode.
SolutionYogi
XHTML transitional without the XML header, we are definitely not in quirks mode. The box model is standard compliant too, so definitely not quirks mode.
Frank Malina
It's actually XHTML 1.0 Strict.
Frank Malina
No really I've runned out of ideas, it will be <!--[if IE gt 6]><a id="toggleVAT"...
Frank Malina
+1  A: 

Just a small thing, IETester cannot read cookies. This might be affecting things. Although, you said you tested in a native IE6, so it's probably not the issue, but it's worth knowing about.

Dan F
This could be actually the root of the problem, orginal implementation I tested on native IE6 didn't have cookies. That was added later.
Frank Malina
Or it might have some other error that I've fixed that rendered it broken.
Frank Malina