views:

51

answers:

3

Hello everyone, i have the following code, which works perfectly in chrome/ff:

chkbx_send_demo.onchange = function(){
                if(sel_template.selectedIndex <= 0 && chkbx_send_demo.checked == true){
                    alert("Choose a Template");
                    sel_template.selectedIndex = 1;
                }
                if(chkbx_send_demo.checked == false){
                    sel_template.selectedIndex = 0;
                }
            }

but just won't work in IE. I've tried to change the event to chkbx_send_demo.onclick and it still won't work.

+1  A: 

Internet Explorer only fires the onchange event when the checkbox loses the focus (onblur). also see here:
http://krijnhoetmer.nl/stuff/javascript/checkbox-onchange/
and here:
http://bytes.com/topic/javascript/answers/92116-onchange-checkbox

GOsha
A: 

Are you sure onclick does not work? Did you check for javascript errors?

The following works in IE7 (don't have IE6 to test)

<html>
    <head>
       <script>
            function text()
            {
                alert(document.getElementById("cbxTest").checked);
            }
       </script>
    </head>
    <body>
        <input type="checkbox" name="cbxTest" id="cbxTest" onclick="text()"/>
        <label for="cbxTest"> Test </label>
    </body>
</html>

Note: This is only for onclick. OnChange works differently in IE, see GOsha's answer.

Nivas
I'm 100% sure it's not working (i put alerts in every step of the onclick function) but strangely there's not javascript errors when i debug the script.
Francisco Freitas
Probably you have already checked, but: are you aure that "chkbx_send_demo" refers to the correct checkbox? Can you do a alert and confirm this?Also, the exact same code workd in FF?
Nivas
i can't believe it, i did an alert(chkbx_send_demo.type); and got a 'hidden'... how can document.getElementById("demo") get this element:<input type="hidden" name="demo" value="0"> instead of this one:<input type="checkbox" id="demo" name="demo" value="1" title='' tabindex="107" > ?? (and why does this only happen in IE??) But anyway, thanks, everything's clear now
Francisco Freitas
So do you say that you had two elements with the same id? then I dont think there is a 'standard' behaviour. Make sure you don't have the same id for different elements on your page.
Nivas
A: 

my JS code is now something like this:

if(navigator.appName == "Microsoft Internet Explorer"){
            alert("IE");
            chkbx_send_demo.onclick = function(){
                alert("HI");
                if(sel_template.selectedIndex <= 0 && chkbx_send_demo.checked == true){
                    alert("Choose a Template");
                    sel_template.selectedIndex = 1;
                }
                if(chkbx_send_demo.checked == false){
                    alert("HI");
                    sel_template.selectedIndex = 0;
                }
                alert("HI");
            }
        }
        else
        {
            chkbx_send_demo.onchange = function(){
                if(sel_template.selectedIndex <= 0 && chkbx_send_demo.checked == true){
                    alert("Choose a Template");
                    sel_template.selectedIndex = 1;
                }
                if(chkbx_send_demo.checked == false){
                    sel_template.selectedIndex = 0;
                }
            }
        }

No javascript errors, but the code just isn't executed on IE and i really can't understand why.

Francisco Freitas
Can you also post your HTML? How did you get `chkbx_send_demo`?
Marcel Korpel