SoFunction
Updated on 2025-02-28

Improved version: Add, modify, and delete option elements in select

After returning from home after taking annual leave today, I saw the "monitoring management demonstration code" posted by Kobayashi on QQ. The core principle uses the add() method of the select element:
Copy the codeThe code is as follows:

function watch_ini(){ // Initial
 for(var i=0; i<; i++){
 var word = ("OPTION");
  = arguments[i];
 (word); // watch. is form name
 }
}
function watch_add(f){ // Add
 var word = ("OPTION");
  = ;
 (word); 
}

However, the above add() method is only valid under IE. To be compatible with FF and Opera, the above code has been improved. The code after the change is as follows:
Copy the codeThe code is as follows:

function watch_ini(){ // Initial
 for(var i=0; i<; i++){
  var oOption=new Option(arguments[i],arguments[i]);
  ("MySelect").options[i]=oOption;
 }
}
function watch_add(f){ // Add
  var oOption=new Option(,);
  []=oOption;
}

The complete code of the entire instance is as follows:

[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]