views:

775

answers:

5

Hi. I've got a JavaScript running 2 ".getElementById('').style" changes. Both work perfectly in FF, but the first one doesn't work in IE7.

Appreciate all help :)

First SourceCode:

function ingen() {
    document.getElementById('merInfo1').style.display='none';
    document.getElementById('merInfo2').style.display='none';
    document.getElementById('merInfo3').style.display='none';
    document.getElementById('merInfo4').style.display='none';
    document.getElementById('merInfo5').style.display='none';
    document.getElementById('merInfo6').style.display='none';
    document.getElementById('merInfo7').style.display='none';
}

HTML:

<span class="enkeltSkjema" id="avtaleInput">
    <select name="radio1" id="radio1">
        <option value="1" onClick="ingen()">lorem</option>
        <option value="2" onClick="some()">lorem</option>                                  
        <option value="3" onClick="other()">lorem</option>
        <option value="4" onClick="func()">lorem</option>
        <option value="5" onClick="tion()">lorem</option>
        <option value="6" onClick="names()">lorem</option>
        <option value="7" onClick="goes()">lorem</option>
        <option value="8" onClick="here()">lorem</option>
    </select>
</span>                          
<span class="enkeltSkjema" id="merInfo">
    <span id="merInfo1" name="merInfo1"></span>
    <span id="merInfo2" name="merInfo2"></span>
    <span id="merInfo3" name="merInfo3"></span>
    <span id="merInfo4" name="merInfo4"></span>
    <span id="merInfo5" name="merInfo5"></span>
    <span id="merInfo6" name="merInfo6"></span>
    <span id="merInfo7" name="merInfo7"></span>
</span>

Second, works fine in IE:

function levAdresse() {
    if (document.getElementById('egenLevAdr').checked == true) {
        document.getElementById('levAdrBox').style.display='block';
    }   
    else {
        document.getElementById('levAdrBox').style.display='none';
    }
}

HTML:

<div id="levAdrBox">
    <div id="levAdr">Field
        <span class="" id="levAdrInput"><input type="text" id="text12" name="text12" /></span>
    </div>
    <div class="" id="levPostSted"> field:*
        <span class="" id="levPostStedInput">
            <input type="text" id="text18" name="text18" />
            <span class="" id="levPostNr">field:*
                <span class="" id="levPostNrInput">
                    <input type="text" id="text17" name="text17" />
                </span>
            </span>
        </span>
    </div>
</div>
+2  A: 

Is it possible you are calling the ingen() function before the DOM is fully constructed?

CodeMonkey1
I don't think so since it's triggered by userinput. Anyway to check this?
A: 

If you have the option/possibility, I'd use jQuery and get rid of (most of) the cross-browser incompatibilities:

function ingen() {
  $("#merInfo span").css("display", "none");
}

$(document).ready(function() {
  $("#egenLevAdr").click(function() { $("#levAdrBox").toggle(); });
});
veggerby
Thanx for the tip! Does Jquery need som sort of script call?
Check out http://jquery.com, it has fairly good docs.
veggerby
Thanx again. Going to check out this alternative.
Checked it out now, but sadly IE is illiterate. Worked fine in FF, as always. Thanx again, nice code.
A: 

Also (this is not an answer to your question) but for the first javascript I think this looks way better:

for(i=1; i<8; i++){
 document.getElementById('merInfo' + i).style.display='none';
}
Pim Jager
Thanx, but the real script has unique names, not numbered ;) Nice use of the for loop!
+2  A: 

Do you have ANY other elements ANYWHERE else on the page with a "name" attribute set to:

merInfo1
merInfo2
merInfo3
merInfo4
merInfo5
merInfo6
merInfo7

(of any case too: e.g. MerInfo1, merinfo1, MERinfo1 are all the same in IE)

if so, that's the bug. IE considers name to be the same as ID (which is a bug obviously)

scunliffe
Yeah, read about that. IE's got some hickups. Have cheched the script, and no. Added a name attribute with the same name, just in case ;) Didn't affect FF in any way.
+4  A: 

Uhhmmm... Maybe if you use "OnChange()" on the SELECT and not "OnClick" on OPTION ?

<select name="radio1" id="radio1" onChange="ingen(this.value)">
        <option value="1">lorem</option>
        <option value="2">lorem</option>                                  
        <option value="3">lorem</option>
        <option value="4">lorem</option>
        <option value="5">lorem</option>
        <option value="6">lorem</option>
        <option value="7">lorem</option>
        <option value="8">lorem</option>
</select>

and in the ingen(something) do a condition, or call other function in relation with the value passed... ?

RVeur23
Correct, onclick is not crossbrowsercompatible on options. http://www.w3schools.com/htmldom/event_onclick.asp
some
doh, can't believe I missed that in the code sample! yes, the onclick on an option in IE will fail. (as will almost any event on an option in IE)
scunliffe