JavaScript Reference/Javascript Methods/add

Материал из Web эксперт
Перейти к: навигация, поиск

"add()" Example

    
<html>
<body>
<script language="JavaScript">
    function function1() {
        var newOption = document.createElement("<option value="TOYOTA">");
        document.all.mySelect.options.add(newOption);
        newOption.innerText = "Toyota";
    }
    function function2() {
        document.all.mySelect.options.remove(0);
    }
</script>
<select id="mySelect">
    <option value="HONDA">Honda</option>
    <option value="ACURA">Acura</option>
    <option value="LEXUS">Lexus</option>
</select>
<input type="button" value="Add" onclick="function1();">
<input type="button" value="Remove" onclick="function2();">
</body>
</html>



"add()" is applied to

Names noted with an asterisk (*) are JavaScript properties only.
+----------------+--------------------------------------------------------------+
| Applied_To     |areas*                          controlRange*                 |
|                |options*                        <select>                      |
+----------------+--------------------------------------------------------------+



"add()" Syntax, Parameters and Note

Note:
Adds an element to the collection.
    
Syntax:
    
document.all.selectID.add(param1, param2)
collectionName.add(param1, param2)

Parameters:
    param1   Required; the element to add.
    param2   Optional; the index position for the added element.