Javascript function not working in IE 7

Can someone tell me how to make this script in IE 7? When I run this, nothing happens.

    <html>
    <head>
    <script language="JavaScript">
    function moveSelectedOption() {
        // Fetch references to the <select> elements.
        var origin = document.getElementById('origin_select');
        var target = document.getElementById('target_select');


        // Fetch the selected option and clone it.
        var option = origin.options[origin.selectedIndex];

       var copy = option.cloneNode(true);

        // Add the clone to the target element.
        target.add(copy, null);
    }
    </script>
</head>
<body>
    <select id="origin_select" multiple>
        <option value="A">A</option>
        <option value="B">B</option>
        <option value="C">C</option>
    </select>
    <select id="target_select" multiple>

        <option value="C1">C1</option>
    </select>
    <button onclick="moveSelectedOption()">Add</button>
 <!--   <input type="button" onClick="moveSelectedOption()" value="AddMeToo" />  this does not work either-->
    </body>
    </html>
+3
source share
2 answers

Try

var origin = document.getElementById('origin_select');
var target = document.getElementById('target_select');

// Fetch the selected option and clone it.
var option = origin.options[origin.selectedIndex];
target.options[target.options.length] = new Option(option.text, option.value);

If you want to remove this option from the source select item, you can use this

origin.remove(option);

Demo without movement

Demo with the move

Edit

This line caused an error.

target.add(copy, null);

add () does not work with the standard second argument in Explorer, even with a null value, so in order to be compatible, you can try the version with two arguments and after the failure, use the version of one argument.

See select.add

0
source

-, add(option, null) IE-up-to-7 - ( " " ). add(option), , , . , select.add(option, options.length) select.options[options.length]= option.

, IE-up-to-7 ( !) - ( "Invalid argument" ), IE <option> , , IE, Windows, , . , IE HTMLOptionElement - , .

, , document.createElement value text, new Option(), rahul.

, selectedIndex <select multiple>. --, . (, Add .) .selected.

+2

Source: https://habr.com/ru/post/1740440/


All Articles