简体   繁体   中英

Javascript (object.innerhtml) is not compatible with IE

I have created a script that changes the combo boxes based on the category chosen. The problem is that the script works in all other browsers aside from Internet Explorer (version 7+). I am not getting an error message, which indicates that IE doesn't like the object.innerhtml. What can I do to solve this?

Working Example: http://adcabinetsales.com/style-chooser.html

function ChangeCabinetCollection() {
    if (document.getElementById("cabinet_collection").value == "broughton") {
        // COPY VALUES
        var first = document.getElementById('broughton_styles');
        var options = first.innerHTML;
        var second = document.getElementById('cabinet_selector');

        // REPLACE VALUES
        second.innerHTML = options;

        // CHANGE CABINET IMAGE TO BE IN THE COLLECTION OF CHOICE
        changeDoor("cabinet_selector");
    } else if (document.getElementById("cabinet_collection").value == "specialty") {
        // COPY VALUES
        var first = document.getElementById('cabinet_style');
        var options = first.innerHTML;
        var second = document.getElementById('cabinet_selector');

        // REPLACE VALUES
        second.innerHTML = options;

        // CHANGE CABINET IMAGE TO BE IN THE COLLECTION OF CHOICE
        changeDoor("cabinet_selector");
    }
}

function ChangeGraniteCollection() {
    if (document.getElementById("granite_collection").value == "new_arrivals") {
        // COPY VALUES
        var first = document.getElementById('granite_new');
        var options = first.innerHTML;
        var second = document.getElementById('granite_selector');

        // REPLACE VALUES
        second.innerHTML = options;

        // CHANGE CABINET IMAGE TO BE IN THE COLLECTION OF CHOICE
        changeGranite("granite_selector");
    } else if (document.getElementById("granite_collection").value == "Specialty Styles") {
        // COPY VALUES
        var first = document.getElementById('specialty_granite_styles');
        var options = first.innerHTML;
        var second = document.getElementById('granite_selector');

        // REPLACE VALUES
        second.innerHTML = options;

        // CHANGE CABINET IMAGE TO BE IN THE COLLECTION OF CHOICE
        changeGranite("granite_selector");
    }
}

It's not .innerHTML in general; it's using it with <select> elements that's causing you problems. There are a couple ways to solve the problem. A simple one is to replace the entire <select> element along with the option list. That is, if your HTML looks like this:

<select name=whatever id=whatever>
  <option value=1>Hello
  <option value=2>World
</select>

Then you could alter that to be:

<span class=select-wrapper>
  <select name=whatever id=whatever>
    <option value=1>Hello
    <option value=2>World
  </select>
</span>

Then just replace the .innerHTML of the <span> wrapper around the <select> .

The other way to solve the problem is to build an array of Option instances and update the "options" property of the <select> element.

I'm not sure of why you would have problems with IE7+, I don't have nay Microsoft products to test with but maybe this bug:

BUG: Internet Explorer Fails to Set the innerHTML Property of the Select Object

But taking @Pointy answer, then you could perform a replace of the select like this, rather than wrap the element, uses Node.cloneNode Node.replaceChild . I believe this is cross-browser.

HTML

<select name="granite_collection" id="granite_collection">
    <option selected="selected">Choose a Collection</option>
    <option value="new_arrivals">Granite: New Arrivals</option>
    <option value="Specialty Styles">Granite: Specialty Styles</option>
</select>
<select name="granite_selector" id="granite_selector"></select>
<select name="cabinet_style" id="cabinet_style" style="visibility:hidden;">
    <option value="images/doorstyles/Brandy wine.jpg">Brandy Wine</option>
    <option value="images/doorstyles/Canvas.jpg">Canvas</option>
    <option value="images/doorstyles/Country Linen.jpg">Country Linen</option>
    <option value="images/doorstyles/Expresso.jpg">Expresso</option>
    <option value="images/doorstyles/Mocha glazed.jpg">Mocha Glazed</option>
    <option value="images/doorstyles/Shaker.jpg">Shaker</option>
    <option value="images/doorstyles/Tahoe.jpg">Tahoe</option>
    <option value="images/doorstyles/Charleston Antique White.jpg">Charleston Antique White</option>
    <option value="images/doorstyles/Charleston Cherry.jpg">Charleston Cherry</option>
    <option value="images/doorstyles/Charleston Saddle.jpg">Charleston Saddle</option>
    <option value="images/doorstyles/Java Shaker.jpg">Java Shaker</option>
    <option value="images/doorstyles/Savannah Toffee.jpg">Savannah Toffee</option>
    <option value="images/doorstyles/White Shaker.jpg">White Shaker</option>
    <option value="images/doorstyles/York Chocolate.jpg">York Chocolate</option>
    <option value="images/doorstyles/York Antique White.jpg">York Antique White</option>
    <option value="images/doorstyles/Dillon1.jpg">Dillon1</option>
    <option value="images/doorstyles/Dillon2.jpg">Dillon2</option>
</select>

Javascript

var graniteCollection = document.getElementById("granite_collection"),
    graniteSelector = document.getElementById("granite_selector"),
    cabinetStyle = document.getElementById("cabinet_style");

graniteCollection.onchange = function () {
    var clone = cabinetStyle.cloneNode(true);

    clone.id = graniteSelector.id;
    clone.name = graniteSelector.name;
    clone.style.visibility = "";
    graniteSelector.parentNode.replaceChild(clone, graniteSelector);
}

On jsfiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM