Javascript JS can move up or down the drop-down list option

Source: Internet
Author: User

Compatible with Firefox, ie, and select option JS that can be moved up, down, top, and bottom. <br/> principle: <br/> top: this mainly refers to switching the option of the selected index and the text and value of the option above the selected index; moving these options down an index, finally, you can assign the text and value of the selected index to 0th options. <br/> move up: moving up refers to exchanging the selected index with the text and value of the selected option-1. <br/> moving down: opposite to going up, add 1 to the selected index selectedindex, that is, the next option of the selected option, and then use the variable to store the text and value of any of the two options for exchange; <br/> bottom: Switch the options from the selected option to the options length to the text and value, and finally switch the selected OPT The text and value of ion can be assigned to the last option. </P> <p> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <br/> <HTML> <br/> <pead> <br/> <title> moveoption.html </title> </P> <p> <meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3 "> <br/> <meta http-equiv =" Description "content =" this is my page "> <br/> <meta http-equiv =" Content-Type" content = "text/html; charset = UTF-8 "> <br/> <script language =" JavaScript "> <br/> <! -- <Br/> function totop () {<br/> var box = document. getelementbyid ("sbox"); <br/> var Index = Box. selectedindex; <br/> If (index> 0) {<br/> var text = Box. options [Index]. text; <br/> var val = Box. options [Index]. value; <br/> for (VAR I = index; I> 0; I --) {<br/> box. options [I]. TEXT = Box. options [I-1]. text <br/> box. options [I]. value = Box. options [I-1]. value; <br/>}< br/> box. options [0]. value = val; <br/> box. Options [0]. TEXT = text; <br/> box. selectedindex = 0; <br/>}</P> <p> function toup () {<br/> var box = document. getelementbyid ("sbox"); <br/> var Index = Box. selectedindex; <br/> var I = index-1; <br/> If (index> 0) {<br/> var text = Box. options [I]. text; <br/> var val = Box. options [I]. value; <br/> box. options [I]. TEXT = Box. options [Index]. text <br/> box. options [I]. value = Box. options [Index]. value; <br /> Box. options [Index]. value = val; <br/> box. options [Index]. TEXT = text; <br/> box. selectedindex = I; <br/>}</P> <p> function todown () {<br/> var box = document. getelementbyid ("sbox"); <br/> var Index = Box. selectedindex; <br/> var I = 0; <br/> If (index! = Box. length-1) {<br/> I = index + 1 <br/> If (index <box. length) {<br/> var text = Box. options [I]. text; <br/> var val = Box. options [I]. value; <br/> box. options [I]. TEXT = Box. options [Index]. text <br/> box. options [I]. value = Box. options [Index]. value; <br/> box. options [Index]. value = val; <br/> box. options [Index]. TEXT = text; <br/> box. selectedindex = I; <br/>}</P> <p> function tofoot () {<br/> var box = document. getelementbyid ("sbox"); <br/> var Index = Box. selectedindex; <br/> var Len = Box. length; <br/> If (index <len-1) {<br/> var text = Box. options [Index]. text; <br/> var val = Box. options [Index]. value; <br/> for (var j = index + 1; j <= len-1; j ++) {<br/> box. options [J-1]. TEXT = Box. options [J]. text <br/> box. options [J-1]. value = Box. options [J]. value; <br/>}< br/> box. options [Len-1]. value = val; <br/> box. options [Len-1]. TEXT = text; <br/> box. selectedindex = len-1; <br/>}< br/> // --> <br/> </SCRIPT> </P> <p> </pead> </P> <p> <body> <br/> <select id = "sbox" size = "10" style = "width: 100px; Height: 150px; "> <br/> <option value = 1> A </option> <br/> <option value = 3> C </option> <br/> <option value = 4> E </option> <br/> <option value = 5> F </option> <br/> <option value = 7> 1 </option> <br/> <option value = 12> 5 </option> <br/> </SELECT> <br/> <input type = "button" value = "TOP" onclick = "totop () "/> <br/> <input type =" button "value =" up "onclick =" toup () "/> <br/> <input type =" button "value =" down "onclick =" todown () "/> <br/> <input type =" button "value =" bottom "onclick =" tofoot () "/> <br/> </body> <br/> </ptml> <br/>

 

 

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.