views:

448

answers:

3

I have an on-screen keyboard in order to provide a safer input for passwords.

The keyboard itself is placed like this:

<div class="teclado_grafico" id="teclado_grafico">
    <a class="tecla_teclado"  onmousedown="teclaAction( this, 'caja_selector'); return false" style="top: 0px; left: 0px;">Q</a>
    <a class="tecla_teclado"  onmousedown="teclaAction( this, 'caja_selector'); return false" style="top: 0px; left: 28px;">W</a>
.
.
.
</div>

And it has a "Shift button" which fires a JS function with this (I've already tried all that, indeed):

if (obj.innerHTML == "Mayus.") {
    try {
        MAYUSCULA_ACTIVADO = !MAYUSCULA_ACTIVADO;
        var tgrafico = document.getElementById("teclado_grafico");
        if(MAYUSCULA_ACTIVADO) {
//            tgrafico.className = "teclado_grafico mayuscula";
//            $("#teclado_grafico").removeClass('minuscula').addClass('mayuscula');
//            $("#teclado_grafico").attr('class', 'teclado_grafico mayuscula');
//            $("#teclado_grafico").attr('className', 'teclado_grafico mayuscula');
            tgrafico.setAttribute('className', "teclado_grafico mayuscula") ||
            tgrafico.setAttribute('class', "teclado_grafico mayuscula");
        } else {
//            tgrafico.className = "teclado_grafico minuscula";
//            $("#teclado_grafico").removeClass('mayuscula').addClass('minuscula');
//            $("#teclado_grafico").attr('class', 'teclado_grafico minuscula');
//            $("#teclado_grafico").attr('className', 'teclado_grafico minuscula');
            tgrafico.setAttribute('className', "teclado_grafico minuscula") ||
            tgrafico.setAttribute('class', "teclado_grafico minuscula");
        }
    } catch (_E) {
        //void
    }
    return;
}

The associated CSS is like this:

.mayuscula a.tecla_teclado{
    text-transform: uppercase;
}
.minuscula a.tecla_teclado{
    text-transform: lowercase;
}

It works on every single browser I've tried. IE 6, 7; Opera 10; GChrome; FF 3, 3.5 and 3.6; Safari 4,... but in IE8 v8 (strict mode) the class is not changed! I mean, debuggin' with the IE8 tools allows one to see that the attribute className is there and it changes... but the user does not see the letters changing from uppercase to lowercase, to uppercase again.

I just don't know how to handle this... I had complains about the client using IE6... now they updated their stuff and this shows up. Any help will be reaaaaly helpful!

EDIT Already tried suggestions of

tgrafico.className = MAYUSCULA_ACTIVADO ? "teclado_grafico mayuscula" : "teclado_grafico minuscula";

but no joy yet. Opening IE8 dev's tools allows one to see in the HTML tab that the class is changing correctly between the expected values, but the browser just does not behave!

+2  A: 

Don't go anywhere near attributes for this, since IE's behaviour with attributes is inconsistent with other browsers and across modes and you don't need to deal with them. Instead, just use the element's className property:

tgrafico.className = MAYUSCULA_ACTIVADO ?
                     "teclado_grafico mayuscula" : "teclado_grafico minuscula";

UPDATE

This appears to be a bug in IE 8. The approach is definitely correct, and the class is getting switched: you can prove this by changing the appropriate CSS and observing the text color changes correctly when you click the shift button:

.mayuscula a.tecla_teclado{
    text-transform: uppercase;
    color: green;
}
.minuscula a.tecla_teclado{
    text-transform: lowercase;
    color: blue;
}

Furthermore, each <a> element's currentStyle.textTransform property is being set correctly, as you can prove using the following:

<a class="tecla_teclado" onclick="alert(this.currentStyle.textTransform);">Q</a>

So we conclude it's a rendering bug in IE 8. I've found a workaround, which is not to apply text-transform on the default state, which is upper case. So using my class-switching code and changing your CSS to

.mayuscula a.tecla_teclado{
}
.minuscula a.tecla_teclado{
    text-transform: lowercase;
}

... will fix it.

Tim Down
Sorry, dude. The wrong behavior refuses to go. It DOES change the first time from uppercase to lowercase BUT then, it just does not work anymore...
Alfabravo
My approach is correct, and there appears to be a bug in IE 8. I've suggested a workaround in my updated answer.
Tim Down
You're right, thanks for your help.
Alfabravo
+1  A: 

I would try to use jquery and basically have all the keys lowercased and have css class

.Upper {text-decoration:uppercase; }

than the following code should work

$('.shift').click(function(){ 
    $('.key').toggleClass('Upper');
});

Actually i have almost the same stuff, but not with text decoration property, and IE8 do update appearance of DOM elements on toggleClass Probably otherwise you should look what jQuery do on toggleClass in order to make it cross browser

vittore
Using the things shown in the question, I've used: $('#teclado_grafico').toggleClass('minuscula');. Right approach to JQuery's way. Thanks a lot.PS. SO doesn't allow to set the bounty yet. Says it can be done in 21 hours. Until then, I leave a +1 :)
Alfabravo
We were trying at first to use plain javascript, but after some problems like you have decided that guys writing stuff as prototype or jquery spend much more time on this and better not to throw others experience =)
vittore
Actually here the fix was removing the `text-transform: lowercase;` from the default state's CSS class and nothing to do with jQuery.
Tim Down
I supposed that the matter is in both lowercase and uppercase applied. But still jquery code looks more neat
vittore
Both answers point to the same, which is going from the two-classes approach to the one-class-toggling approach. As I stated in the update to the question, it does change the classname (yes, I've seen it too) but the rendering just does not work properly. I'll give the bounty to the first one giving a working answer, just as I said I will.Thank you both for your help, it's nice to find thoughtful feedback and also finding out that I'm not crazy about IE8 not rendering things! :)
Alfabravo
Have I not given a working answer?
Tim Down
You have, once you found the problem in IE8 and corrected your suggestion. I told you already that you're right and I said thank you. And I meant it. But we should be fair AND the first one was our pal vittore. No bad feelings, dude :)
Alfabravo
No problem at all.
Tim Down
A: 

I have that situation with IE8. My solution is:

tgrafico.className ="teclado_grafico mayuscula";

NOT throught setAttribute('className'...

I don't know why this is that.

P.S. Sorry my english... :)

DeniZ
As I have shown in the code included in the question, that's the first thing I tried and it does not work on IE8 strict mode as far as I know. Thanks for your help... I'll set an online test so we can check every case.PS. Don't worry, it's ok :)
Alfabravo