Home Web Front-end JS Tutorial JavaScript implements two select drop-down box options to move left and right

JavaScript implements two select drop-down box options to move left and right

Mar 30, 2018 am 11:20 AM
javascript js select

This article introduces JavaScript code to implement two select drop-down box options to move left and right. Interested students can refer to this article

I have nothing to do today and I wrote a drop-down with two select using js. A small program that can move elements between boxes. I have just started learning JavaScript and there are still many areas for optimization.

Javascript code

The above is the javascript code, and the following is the html plus css code.
The code is as follows

<script type="text/javascript">

 

     

  /**Move the selected element to the right**/

functionmoveRight()

{

         

                                                                                                        use using use using                out out out ’s ’ ’s ’ through out ’s through out ‐ through out ‐ through ‐ ‐‐‐‐ and, to ");

varoptionElements = selectElement.getElementsByTagName("option");

varlen = optionElements.length;

if(!(selectElement.selectedIndex==-1)) //If no element is selected, then selectedIndex will be -1

                                                                                                                                  SelectedIndex                ‐‐‐1 select object

varselectElement2 = document.getElementById("secend");

// Move to the right

for(vari=0;i&lt ) # {

alert("You have not selected the element to move!");

}

}

/ /Move all to the right

functionmoveAll()

{

//Get the first select object

varselectElement = document.getElementById("first ");

varoptionElements = selectElement.getElementsByTagName("option");

varlen = optionElements.length;

//alert(len);

​ //Flip the array in the first selected

​ varfirstOption =newArray();

​​ for(vark=len-1;k>=0;k -) ;

//Get the second select object

varselectElement2 = document.getElementById("secend");

for(varj=lens-1;j>=0 )

# //Move the selected element to the left

functionmoveLeft()

{

//First get the second select object

varselectElement = document.getElementById("secend");

varoptionElement = selectElement.getElementsByTagName("option");

varlen = optionElement.length;

      

         // Get the first element again

       if (!(selectElement.selectedIndex==-1))

                                                                          varfirstSelectElement = document.getElementById("first");

for(i=0;i<len;i++)

{

firstSelectElement.appendChild(optionElement[selectElement. selectedIndex]);//The index of the selected element

}

}else

{

alert("You have not selected anything to move. s project!");

}

}

 

  //Move all to the left

  functionmoveAllLeft()

      varselectElement = document.getElementById( "secend");

varoptionElements = document.getElementsByTagName("option");

varlen = optionElements.length;

varoptionEls =newArray();

for(vari=len-1;i>=0;i--)

{

optionEls.push(optionElements[i]);

}

varlens = optionEls.length;

varfirstSelectElement = document.getElementById("first");

for(varj=lens-1;j> =0;j--)

{

firstSelectElement.appendChild(optionEls[j]);

}

}

</script>

Html code

The code is as follows<html><head>

<metahttp-equiv="Content-Type"content="text/html; charset=utf-8"/>

<title>Untitled Document</title>

<styletype="text/css">

.select_move { margin:0px auto; width:206px; height:140px; margin-top:300px;}

.select_move_1 { float:left;}

.select_move_2 { float:left; }

.select_move_3 { float:left;}

</style>

</head>

&lt ;body>

  <pclass="select_move">

   

                                                 first"size="10"id="first"multiple="multiple">

                                                                                                            "Toyota">Toyota</option>

                                                                                                                                                        < /option> #                                                                                                             <pclass="select_move_2">

# & lt; inputtype = "Button" Value = "=== & GT;" OnClick = "Moveall ()" /& GT; & LT;

& LT; "value="<------"onclick="moveLeft()"/><br/>

                                                                                   "onclick="moveAllLeft()"/>

                                                                                                           id="secend"multiple="multiple">

                                                                          # ​

</body>

</html>

##

Related recommendations:

Sample code sharing using JavaScript to implement two select drop-down box options to move left and right

The above is the detailed content of JavaScript implements two select drop-down box options to move left and right. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to implement an online speech recognition system using WebSocket and JavaScript

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Recommended: Excellent JS open source face detection and recognition project

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Dec 17, 2023 pm 06:55 PM

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

How to use JavaScript and WebSocket to implement a real-time online ordering system

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

Simple JavaScript Tutorial: How to Get HTTP Status Code

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecasting system

See all articles