JS Operation Select and option

Source: Internet
Author: User

1. Dynamic creation of SELECT

function Createselect () {  var myselect = document.createelement_x ("select");      = "Myselect";      Document.body.appendChild (Myselect);}

2. Add Options option

function addoption () {          // find objects by ID,          var obj=document.getelementbyidx_x (' Myselect ');           // Add an option          Obj.add (new Option ("text", "value"));    // This only works          in IE. Obj.options.add (new// this compatible with IE and Firefox}        

3. Remove all options option

function RemoveAll () {            var obj=document.getelementbyidx_x (' Myselect ');            Obj.options.length=0;}     

4. Delete an option

function Removeone () {            var obj=document.getelementbyidx_x (' Myselect ');             // index, to remove the ordinal of the option, take the ordinal            of the currently selected option var index=Obj.selectedindex;            Obj.options.remove (index);}        

5. Get the value of option options

var obj=document.getelementbyidx_x (' myselect 'var// sequence number, whichever is the currently selected option var val = obj.options[index].value;

6. Get the text of options option

var obj=document.getelementbyidx_x (' myselect 'var// sequence number, whichever is the currently selected option  var val = obj.options[index].text;

7. Modify Options option

var obj=document.getelementbyidx_x (' myselect 'var// sequence number, whichever is the currently selected option var val = obj.options[index]= new Option ("Fresh Text", "New value");

8. Delete Select

function Removeselect () {       var myselect = document.getelementbyidx_x ("Myselect");       MySelect.parentNode.removeChild (Myselect);}

The complete code for the entire instance is as follows:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01//ZH-CN" "HTTP://WWW.W3.ORG/TR/HTML4/STRICT.DTD" > 

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

functionWatch_ini () {//Initial      for(vari=0; i<arguments.length; i++){         varWord = document.createelement_x ("OPTION"); Word.text=Arguments[i]; Watch.keywords.add (word); //watch. is form name     }}functionWatch_add (f) {//Increase     varWord = document.createelement_x ("OPTION"); Word.text=F.word.value; F.keywords.add (word); However, the above add () method is only valid under IE, for the compatibility of FF and opera, the above code is improved, the following code is changed:functionWatch_ini () {//Initial     for(vari=0; i<arguments.length; i++){        varooption=NewOption (Arguments[i],arguments[i]); Document.getelementbyidx_x ("Myselect") [i]=ooption; }}functionWatch_add (f) {//Increase    varooption=NewOption (F.word.value,f.word.value); F.keywords[f.keywords.length]=ooption;}

The complete code for the entire instance is as follows:

<!doctype HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" > functionWatch_ini () {//Initial  for(vari=0; i<arguments.length; i++){    varooption=NewOption (Arguments[i],arguments[i]); Document.getelementbyidx_x ("Myselect") [i]=ooption;} } functionWatch_add (f) {//Increase    varooption=NewOption (F.word.value,f.word.value); F.keywords[f.keywords.length]=ooption;} functionWatch_sel (f) {//EditF.word.value =F.keywords[f.keywords.selectedindex].text;} functionWatch_mod (f) {//ModifyF.keywords[f.keywords.selectedindex].text =F.word.value;} functionWatch_del (f) {//DeleteF.keywords.remove (f.keywords.selectedindex);} functionWatch_set (f) {//Save varSet = "";  for(vari=0; i<f.keywords.length; i++) {Set+ = F.keywords[i].text + ";"; } Confirm (set); } // -</script> Watch_ini ("I", "You", "You", "he", "she", "it", "er");//Initial keywords // -</script> <input type= "text" name= "word"/><br/> <input type= "button" value= "Add onclick=" Watch_ Add (this.form); "/> <input type=" button "value=" Modify "onclick=" Watch_mod (this.form); "/> <input type=" button "Value=" delete "onclick=" Watch_del (this.form), "/> <input type=" button "value=" Save "onclick=" Watch_set (this.form); " /> </form>

Create an array method with a string:

<script language= "JavaScript" > </body> functionspli () {datastr= "2,2,3,5,6,6"; varStr=NewArray (); STR=datastr.split (",");  for(I=0;i<str.length; i++) {document.write (Str[i]+ "<br/>");    }} spli (); </scripttoday debugging a section of JS, in IE, running well, debugging errors on Firefox, check data found Obj.add (NewOption ("text", "value"));//This only works in IE.Obj.options.add (NewOption ("text", "value");//this compatible with IE and Firefox

JS Operation Select and option

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.